Using the webdriver API for cookie management and javascript execution


#1

Hello,

is there a code example available which shows how to interact with webdriver API? Our tests need to be able to execute javascript and read cookies for session management (driver.manage().getCookies() and the JavaScriptExecutor interface).

I saw this code example:

import com.xceptance.xlt.engine.util.DefaultWebDriverFactory;

...
public class TBrowse extends AbstractWebDriverScriptTestCase {
  public TBrowse() {
    super(DefaultWebDriverFactory.getWebDriver(), "http://example.org");
  }
}

But also it was mentioned, that starting with XLT 4.10.0, the explicit call to our DefaultWebDriverFactory won’t be necessary anymore. How does it work with XLT 4.10.0+?

I also saw in the release notes following paragraphs:

XltFirefoxDriver / XltChromeDriver reusable – The XLT framework 
can be configured to reuse a Web driver instance for multiple test
cases (see property xlt.webDriver.reuseDriver) saving the overhead of
starting the browser again and again for each test case invocation.
This feature is now supported also for 
XltChromeDriver [...] and XltFirefoxDriver [...].

Reuse Web drivers per thread – Now the framework 
maintains not just a global Web driver instance but a separate one for 
each thread when reusing Web drivers. This allows to reuse drivers also 
when executing test cases in parallel (in functional tests or load 
tests).

Is there an example how to use this feature?

Thanks and regards,
Michael


#2

Hi Michael,

to get access to the underlying WebDriver instance from your Java-based script test cases, you would simply call the getWebDriver() method. Test case classes (that extend AbstractWebDriverScriptTestCase) and modules classes (that extend AbstractWebDriverModule) inherit this method automatically. All other classes may use the static helper method StaticScriptCommands.getWebDriver().

Regarding the call to the DefaultWebDriverFactory, the code would look like this, i.e. no constructor needed any longer:

public class TBrowse extends AbstractWebDriverScriptTestCase
{
    @Test
    public void test() throws Throwable
    {
        open("https://localhost:8443/posters");
    }
}

In this case, the call to the DefaultWebDriverFactory will be done internally.

WebDriver instances (created by XLT) will be reused per thread automatically if you set the property xlt.webDriver.reuseDriver = true in the configuration of your test suite, e.g. in file config/project.properties. There is nothing more to do here.

Hope this helps!

Best,
J.


#3

Thanks for the details, now it works!