如何处理多个步骤定义文件中黄瓜硒(How to handle multiple step defin

2019-10-16 13:22发布

我有两个步骤定义文件,并在未来,将有多个,我不能执行我的代码,注销,其做工精细的登录名和一个步骤定义文件,它不断开放的Chrome浏览器。

我创建了使用页面工厂的框架,下面是我的代码:

登录页面:

public class LoginPage {

    WebDriver driver;

    public LoginPage(WebDriver driver) {
        //this.driver=driver;
        PageFactory.initElements(driver, this);

    }

    @FindBy(how=How.ID,using="username")
    public WebElement usernametexbox;

    @FindBy(how=How.ID,using="pass")
    public WebElement passwordtextbox;

    @FindBy(how=How.ID,using="submit")
    public WebElement signin;

    @FindBy(how=How.XPATH,using="//button[@class='btn']")
    public WebElement acceptbutton;

   public void enter_username(String username) {

    usernametexbox.clear();
    usernametexbox.sendKeys(username);
    usernametexbox.getText();
    }

    public void enter_password(String password) {

        passwordtextbox.clear();
        passwordtextbox.sendKeys(password);
    }

    public void clickToSigninbutton() {
        signin.click();
    }

    public void clickToAcceptbutton() {


    acceptbutton.click();

    }

public void fill_LoginDetails() {

    enter_username("abc");
    enter_password("def45");

  }

}

注销页面:

public class LogoutPage {

    WebDriver driver;

    public LogoutPage(WebDriver driver) {
        //this.driver=driver;
        PageFactory.initElements(driver, this);

    }

    @FindBy(how=How.XPATH,using="//*[@class='icon']")
    public WebElement chevron;

    @FindBy(how=How.XPATH,using="//a[@class='logout-link']")
    public WebElement logoutlink;

    public void clickTochevron() {
        chevron.click();
    }

    public void clickToLogoutLink() {


        link.click();
    }
}

属性文件阅读器:

public class PropertiesFileReader {

    public Properties getproperty() throws IOException {
    FileInputStream inputstream=null;
    Properties properties=new Properties();
    try {
        properties.load(new FileInputStream("resources/config.properties"));
    }catch(Exception e) {
        System.out.println("Exception " +e);
    }

        return properties;
    }
}

浏览功能:

public class BrowserUtility {

    public static WebDriver openBrowser(WebDriver driver, String browserName, String url) throws InterruptedException {

        if(browserName.equals("chrome")) {

            System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
            driver=new ChromeDriver();
            driver.manage().window().maximize();
            driver.get(url);
            driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
            return driver;

        }else if(browserName.equals("IE")) {
        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.get(url);
        Thread.sleep(5000); 
        return driver;
    }else if(browserName.equals("Firefox")) {
        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.get(url);
        Thread.sleep(5000); 
        return driver;
    }
        return driver;

}
}

登录Stepdef:

public class StepDefinition {

    public static WebDriver driver;

//  public LoginPage loginpage;

//  Properties properties=new Properties();
    PropertiesFileReader obj=new PropertiesFileReader();

    @Given("^Open browser and enter url$")
    public void open_browser_and_enter_url() throws Throwable {

    Properties properties=obj.getproperty();
    driver= BrowserUtility.openBrowser(driver, properties.getProperty("browser.Name"), properties.getProperty("URL"));

    }

    @Then("^Enter username and password$")
    public void enter_username_and_password() throws Throwable {

    LoginPage loginpage=new LoginPage(driver);
    loginpage.fill_LoginDetails();  
            }

    @Then("^click on sign in button$")
    public void click_on_sign_in_button() throws Throwable {

        new LoginPage(driver).clickToSigninbutton();
        System.out.println("Sign-In successfully");

    }

    @Then("^Terms and conditions page open and click on Accept button$")
    public void terms_and_conditions_page_open_and_click_on_Accept_button() throws Throwable {


        new LoginPage(driver).clickToAcceptbutton();
    }

}

注销stepdef:

public class Logoutstepdef {

    public static WebDriver driver;
    PropertiesFileReader obj=new PropertiesFileReader();

    @Given("^Chevron near username$")
    public void chevron_near_username() throws Throwable {

        Properties properties=obj.getproperty();
        driver= BrowserUtility.openBrowser(driver, 
 properties.getProperty("browser.Name"), properties.getProperty("URL"));

        LogoutPage logoutpage=new LogoutPage(driver);
        logoutpage.clickTochevron();
    }

    @Then("^click on chevron and it should get expands$")
    public void click_on_chevron_and_it_should_get_expands() throws 
 Throwable {

        System.out.println("when user click on checvron it should 
           further expands a window");

    }

    @Then("^click on Logout link$")
    public void click_on_Logout_link() throws Throwable {

        new LogoutPage(driver).clickToLogoutLink();

    }

}

预期结果:应用程序应该得到不同的步骤定义文件成功自动化且只有一个浏览器应该在同一时间得到开通。

实际结果:我有两个步骤定义文件,并在未来将有多个,我不能执行我的代码,注销,其做工精细的登录名和一个步骤定义文件,它不断开放的Chrome浏览器。

Answer 1:

Then一个步骤是创建页面的另一个实例,这就是为什么你会得到多个浏览器打开。

尝试在做这个Logoutstepdef类:

public class Logoutstepdef {

    public static WebDriver driver;
    PropertiesFileReader obj=new PropertiesFileReader();
    private LogoutPage logoutpage; 

    .....//the rest remains the same, until:

    @Then("^click on Logout link$")
    public void click_on_Logout_link() throws Throwable {

    logoutpage.clickToLogoutLink();

    }
}

有类似的问题在这里和这里 。



Answer 2:

我建议尝试与QAF小黄瓜 。 随着QAF你没有需要管理的驱动程序或不需要担心页。 所有你需要扩展WebdriverTestPage到你的页面类和你做。 您也可以在页面类本身步骤。 例如,QAF您的注销页面看起来象下面这样:

public class LogoutPage extends WebDriverBaseTestPage<WebDriverTestPage>{

    @FindBy(locator="xpath=//*[@class='icon']")
    public WebElement chevron;

    @FindBy(locator="xpath=//a[@class='logout-link']")
    public WebElement logoutlink;

    public void clickTochevron() {
        chevron.click();
    }

    //@Then("^click on Logout link$") //you can use cucumber or qaf step annotation
    @QAFTestStep(description="click on Logout link")
    public void clickToLogoutLink() {

        link.click();
    }
}

浏览器管理(打开/关闭浏览器)是由QAF所以根据您的照顾配置 (连续/并行)QAF将提供线程安全的驱动器会话。

此外, 定位器库可以消除页面类的一层。 有可用的,你可以直接使用内置的步骤。 例如:

logoutpage.propeties

chevron.icon.loc=xpath=//*[@class='icon']
logout.link.loc=xpath=//a[@class='logout-link']

在您的BDD你可以直接使用内置的步骤如下:

Scenario: name of scenario
   Given ...
   When ...
   Then verify 'chevron.icon.loc' is present
   And click on 'logout.link.loc'

为了使您的定位自我描述,你可以往前走一步,如下:

logout.link.loc=xpath={"locator":"//a[@class='logout-link']","desc":"logout button"}

描述将在报告中使用,使其更有意义。 还有很多其他的功能是用于功能测试自动化的关键。



文章来源: How to handle multiple step definition file in Cucumber with Selenium