如何使用Selenium处理Cucumber中的多步定义文件

时间:2019-04-12 08:46:05

标签: selenium-webdriver cucumber

我有两个步骤定义文件,将来还会有多个,我将无法执行我的代码以进行注销,无法正常运行以进行登录和一个步骤定义文件,并且会不断打开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浏览器。

2 个答案:

答案 0 :(得分:0)

您的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

答案 1 :(得分:0)

我建议尝试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,您可以消除一层页面类。您可以直接使用内置步骤。例如:

登出页面。属性

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

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