How to handle multiple step definition file in Cuc

2019-08-02 16:03发布

I have two step definition files and in future, there will be multiple, I am unable to execute my code for logout, its working fine for login and one step definition file, and it continuously open chrome browser.

I have created a framework using page factory, below is my code:

Login Page:

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");

  }

}

Logout Page:

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();
    }
}

Property file reader:

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

Browser utility:

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;

}
}

Login 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();
    }

}

Logout 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();

    }

}

Expected Results: Application should get automated successfully for different step definition files and only one browser should get opened at a time.

Actual Results: I have two step definition file and in future there will be multiple, I am unable to execute my code for logout, its working fine for login and one step definition file, and it continuously open chrome browser.

2条回答
Explosion°爆炸
2楼-- · 2019-08-02 16:45

i would suggest to try gherkin with qaf. With QAF you don't need to manage driver or don't need to worry about page. All you need to extend WebdriverTestPage to your page class and you are done. You also can have steps in Page class itself. For example, with qaf your logout page may look like below:

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();
    }
}

Browser management (opening/closing browser) is taken care by QAF so depending on your configuration (sequential/parallel) qaf will provide thread safe drive sessions.

Furthermore, with locator repository you can eliminate one layer of page class. There are in-built steps available you can directly use. For example:

logoutpage.propeties

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

in your BDD you can directly use in-built steps as below:

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

To make your locator self descriptive, you can go one step ahead as below:

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

The description will be used in reporting to make it more meaningful. There are lots of other feature that are crucial for functional test automation.

查看更多
混吃等死
3楼-- · 2019-08-02 16:47

Your Then step is creating another instance of the page, that's why you get multiple browsers open.

Try doing this in your Logoutstepdef class:

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();

    }
}

There are similar questions here and here.

查看更多
登录 后发表回答