执行转轮类时代码中的Click方法正在跳过

时间:2017-05-10 00:26:16

标签: java selenium cucumber

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

当我执行跑步者课程时,我能够打开浏览器,但点击方法是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 
    {

    }

1 个答案:

答案 0 :(得分:0)

您可以查看的内容很少:

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