Serenity... how to get the Webdriver?

10,282

PageObject has a getDriver() method, which you should use if you require the use of WebDriver.

I strongly suggest you study the Serenity BDD's Manual if you are going to work with the framework. You should find an answer to most questions there. Alternatively, check Serenity BDD Users Group.

Share:
10,282
know-nada
Author by

know-nada

Updated on June 04, 2022

Comments

  • know-nada
    know-nada almost 2 years

    I just created a sample project using Serenity + cucumber + java + maven ( > mvn archetype:generate -Dfilter net.serenity-bdd:serenity-cucumber) .

    I can see when it brings up a firefox browser and goes to this website:

    @DefaultUrl("http://en.wiktionary.org/wiki/Wiktionary")
    

    Question: From this point, a "WebDriver driver" was created, but how can I obtain/get to that "driver" variable ? I'm trying to get the window ID using driver.getWindowHandle().

    Updated: The statements are as followed:

    @DefaultUrl("http://en.wiktionary.org/wiki/Wiktionary")
    public class DictionaryPage extends PageObject {...}
    

    I looked up the PageObject super class and found this:

    public abstract class PageObject extends net.serenitybdd.core.pages.PageObject {
    
            protected PageObject() {
            super();
        }
    
        protected PageObject(WebDriver driver, Predicate<? super net.serenitybdd.core.pages.PageObject> callback) {
            super(driver, callback);
        }
    
        public PageObject(WebDriver driver, int ajaxTimeout) {
            super(driver, ajaxTimeout);
        }
    
        public PageObject(WebDriver driver) {
            super(driver);
        }
    }
    

    So.. the PageObject uses the webdriver variable from another super class. What is the syntax I need to declare to get to this "web driver" variable? Thanks Know-nada =========================

    JDelorean!

    Thanks a lot for your help. Here are the codes.. Would you please help tp point out what is wrong with the "driver"?

    1 - Feature File: Feature: Amazon user login authentication Scenario: Amazon user login authentication Given user is on the "home" page

    2 - Step Definitions File: package com.XXXX.steps; import net.thucydides.core.annotations.Steps; import com.XXXX.steps.serenity.User; import cucumber.api.java.en.Given;

     class DefinitionSteps {
        @Steps
        User user;
    
        @Given("^user is on the \"([^\"]*)\" page$")
        public void user_is_on_the_Amazon_page(String pageName)throws Throwable{
            user.is_on_the_page(pageName);
        }
    }
    

    3 - Test Runner class package com.XXXX;

    import cucumber.api.CucumberOptions;
    import net.serenitybdd.cucumber.CucumberWithSerenity;
    import org.junit.runner.RunWith;
    
    @RunWith(CucumberWithSerenity.class)
    @CucumberOptions(features="src/test/resources/features")
    public class DefinitionTestSuite {
    

    }

    4 - The User Class where I am not able to capture the driver. Please note that at the end of this file, the commented out line is the one that does not work. Also notice that the "driver" was set to the original driver "home.getDriver()" but somehow that value was passed to the "driver" but the driver "home.getDriver()" works just fine.

    package com.XXXX.steps.serenity;
    
    import com.XXXX.pages.Home;
    import net.thucydides.core.annotations.Step;
    import net.thucydides.core.steps.ScenarioSteps;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    
    public class User extends ScenarioSteps{
    
        Home home = new Home();
        WebDriver driver = home.getDriver();
    
        @Step
        public void is_on_the_page(String pageName) throws Throwable {
            home.open();
    
    home.getDriver().findElement(By.xpath(home.homeElements.get("Hello. Sign in"))).click();
            // driver.findElement(By.xpath(".//*[@id='nav-link-yourAccount']/span[1]")).click();
        } 
    }