Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行程序类执行时跳过代码中的Click方法_Java_Selenium_Cucumber - Fatal编程技术网

Java 运行程序类执行时跳过代码中的Click方法

Java 运行程序类执行时跳过代码中的Click方法,java,selenium,cucumber,Java,Selenium,Cucumber,我在page包中创建了基类,它位于src/test/java/stepDefinitions/pages下。在页面包中创建了帐户类(页面对象)。在StepDefinition包中创建了Account steps类(步骤定义文件)、Runner类 当我执行runner类时,我能够打开浏览器,但是click方法是selenium基类没有发生 //Selenium base class: package Stepdfinations.pages; import java.io.F

我在page包中创建了基类,它位于src/test/java/stepDefinitions/pages下。在页面包中创建了帐户类(页面对象)。在StepDefinition包中创建了Account steps类(步骤定义文件)、Runner类

当我执行runner类时,我能够打开浏览器,但是click方法是selenium基类没有发生

//Selenium base class:

    package Stepdfinations.pages;


    import java.io.File;
    import java.net.URL;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.Properties;
    import java.util.concurrent.TimeUnit;

    import org.apache.commons.logging.Log;
    import org.apache.log4j.Logger;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.Test;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    public class SeleniumBaseclass 
    {


        Properties prop;
        public HttpURLConnection connection = null;
        String currenturl;

        protected WebDriver driver;
        public SeleniumBaseclass (WebDriver driver)
        {
            this.driver=driver;
        }
     public void Property() throws Exception
        {




                File f= new File("C:\\Users\\watareuman9\\workspace\\Cucumberproject\\src\\test\\resources\\data\\config.property");

                FileInputStream fis = new FileInputStream(f);
                prop=new Properties();

                       prop.load(fis);
                       System.setProperty("webdriver.chrome.driver", getChromespath());
                       driver=new ChromeDriver();
                    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

                    driver.get(getAppurl());
                    Thread.sleep(500);


        }
                       public String getChromespath()
                       {
                               return prop.getProperty("ChromePath");
                       }

                       public String getAppurl()
                       {
                               return prop.getProperty("URL");
                       }

                       //click method

                       public void click(String objstr,WebElement objname)

                       {try
                       {
                           Thread.sleep(5000);
                           objname.click();


                           System.out.println("'"+objstr+"'"+"is clickde");
                       }
                       catch(Exception e)
                       {
                           getHttpResponse();
                       }
                       }

                       public void getCurrenturl()
                       {
                           String currenturl=driver.getCurrentUrl();
                       }

                      public void  getHttpResponse()
                      {
                          try
                          {
                              getCurrenturl();

                              URL url=new URL(currenturl);
                              HttpURLConnection connection=(HttpURLConnection)url.openConnection();
                              connection.setConnectTimeout(3000);
                              connection.connect();
                              if(connection.getResponseCode()==200)
                              {
                                  System.out.println(currenturl +"-"+connection.getResponseMessage());

                              }
                              else if(connection.getResponseCode()==connection.HTTP_NOT_FOUND)
                              {
                                  System.out.println(currenturl +"-"+connection.getResponseMessage());
                              }
                          }
                              catch(Exception e)
                              {
                                  e.getMessage();
                              }
                      }
                          public void getQuitdriver()
                          {
                              driver.close();
                          }
                      }


    //Account class(page objects)

    package Stepdfinations.pages;


    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.support.How;

    public class Account extends SeleniumBaseclass
    {
        public Account(WebDriver driver)
        {
            super(driver);

            this.driver=driver;
        }

        @FindBy(how = How.XPATH, using = "//*[@id='bodyContent']/div/div[1]/a[1]/u")
         private WebElement login_button;
    public void clickLoginButton() 
    {
    click("login_button",login_button);


    }
    }


    //Accountsteps(Step defination file)

   package Stepdfinations;


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

    import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;

import org.openqa.selenium.support.FindBy;
    import org.openqa.selenium.By;
    import Stepdfinations.pages.Account;

    import Stepdfinations.pages.SeleniumBaseclass;

    public class Acoountsteps
    {
        WebDriver driver;
        Account act;
        @Given("^I navigated to the Login page$")
        public void i_navigated_to_the_Login_page() throws Throwable
        {
             act=new Account(driver);
             act.Property();
        }
        @When("^I click on the New Account link$")
        public void i_click_on_the_New_Account_link() throws Throwable 
        {
            act.clickLoginButton();


        }

    }



    //Runner class



    package Stepdfinations;

    import org.junit.runner.RunWith;
    import cucumber.api.junit.Cucumber;
    import cucumber.api.CucumberOptions;

    @RunWith(Cucumber.class)
    @CucumberOptions(features="src/test/resources/features")

    public class Runner 
    {

    }

你可以研究的事情很少:

  • 检查您的x路径(您可以使用FireBug)
  • 试着调试你的代码-调试器是否登陆到你的测试
  • 您可能需要在runner类中提到测试类或测试方法(例如,在testNG中,如果代码是通过testNG runner运行的,那么您需要在xml文件中提到测试类或测试方法,testNG使用什么执行什么不执行)

我已经提供了所有信息。我只是想知道我需要提供什么额外的步骤定义类丢失了。您已复制accountpo两次。你的xpath正确吗?urlconnection代码的用途是什么?我添加了步骤定义类,是的,我的xpath很好