【问题标题】:How to handle multiple step definition file in Cucumber with Selenium如何使用 Selenium 在 Cucumber 中处理多个步骤定义文件
【发布时间】:2019-04-12 08:46:04
【问题描述】:

我有两个步骤定义文件,将来会有多个,我无法执行我的注销代码,登录和一个步骤定义文件工作正常,并且它不断打开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();
    }

}

注销步骤定义:

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浏览器。

【问题讨论】:

  • BrowserUtility.openBrowser() 始终打开 Chromedriver,无论浏览器名称如何。 (注意:只是注意到,而不是试图回答您的问题 - 尚未)。
  • 我的问题是否不同不仅与打开浏览器有关,如果您知道如何处理黄瓜中的多个步骤定义
  • 你为什么在你的页面对象类中注释掉this.driver=driver;?这需要坚持下去,否则如果它完全有效,你会得到非常不可预测的结果。

标签: selenium-webdriver cucumber


【解决方案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();

    }
}

herehere 也有类似的问题。

【讨论】:

  • 感谢 Mate,它有助于停止打开多个浏览器。一旦我成功地自动化登录页面和条款和条件页面,尽管它写在一步 def 文件中,并且注销功能写在 logoutstepdef 类中,当我执行我的代码时,第二个浏览器停止在登录页面并且测试用例失败,我们不能在 cucumber 中扩展 stepdef 但我们可以使用 picocontainer ..你知道如何使用 picocontainer 来集成第一步 def 然后第二步来执行端到端测试。
  • 如果您觉得这个答案有用,您可以点赞。您还有其他问题吗?
【解决方案2】:

我建议尝试gherkin with qaf。使用 QAF,您无需管理驱动程序或无需担心页面。您只需将WebdriverTestPage 扩展至您的页面类即可。您也可以在 Page 类本身中有步骤。例如,使用 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 负责,因此取决于您的 configuration(顺序/并行)qaf 将提供线程安全驱动会话。

此外,使用locator repository,您可以消除一层页面类。您可以直接使用内置步骤。例如:

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"}

描述将用于报告以使其更有意义。 还有许多其他功能对于功能测试自动化至关重要。

【讨论】:

  • 这对我没有帮助我得到空指针异常,我创建了一个包 Stepdef,它有两个不同的类登录和注销,它可以正常登录但不能用于注销,我正在注销空指针异常虽然我们不能扩展stepdefinition bcs cucumber不允许..你有什么解决方案来处理注销的空指针异常吗,我的期望是当我运行我的代码时它成功登录然后注销
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
  • 2017-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多