2017-05-10 25 views
0

我已經在src/test/java/Stepdefinations/pages下創建了頁面包中的基類。在頁面包中創建帳戶類(頁面對象)。創建帳戶步驟類(步驟定義文件),在stepdefination包中的Runner類。執行runner類時代碼中的Click方法跳過

當我執行跑步者類時,我可以打開瀏覽器,但點擊方法是硒基類沒有發生。

//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 
    { 

    } 
+0

[請添加最小的,完整的,並且覈查示例](https://stackoverflow.com/help/mcve) –

+0

我所提供的所有信息。我只是想知道我需要提供多少額外的功能 –

+0

步驟定義類缺失。您已複製兩次帳戶。你的xpath是否正確? urlconnection代碼的目的是什麼? – Grasshopper

回答

0

一些事情你可以看看:

  • 檢查你的x路(你可以用螢火吧)
  • 嘗試調試代碼 - 是調試器降落到你考試的情況。
  • 可能需要將測試類或測試方法提交到runner類(例如,在testNG中,如果代碼是通過testNG runner運行的,則需要將test或class提到xml文件中,testNG使用執行的內容和什麼不)。