Using Selenide with Sauce labs (remote Web Driver)

1,048 views
Skip to first unread message

Mark Winspear

unread,
Oct 5, 2015, 5:23:44 AM10/5/15
to selenide
Hi

My existing framework uses Selenium and integrates with Sauce labs to test on multiple OS, browsers, devices in the cloud

It has a Config file which sets some constants and default values which can be overridden at runtime.
It has a Base test class (below) which sets Desired Capabilities contains some JUnit rules and Test Watchers to update the Sauce labs test statuses which is then extended by test classes (example below)

Can anyone suggest how I can modify these tests to use Selenide in Sauce labs please?

package Tests;

import PageObjects.Config;
import com.saucelabs.saucerest.SauceREST;
import org.junit.Rule;
import org.junit.rules.ExternalResource;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

public class Base implements Config {

    protected WebDriver driver;
    private String testName;
    my private String sessionId;
    private SauceREST sauceClient;

    @Rule
    public ExternalResource resource = new ExternalResource() {

        @Override
        protected void before() throws Throwable {
            if (host.equals("saucelabs")) {
                DesiredCapabilities capabilities = new DesiredCapabilities();
                capabilities.setCapability("browserName", browser);
                capabilities.setCapability("version", browserVersion);
                capabilities.setCapability("platform", platform);
                capabilities.setCapability("name", testName);           //sets test name in saucelabs to the test name
                capabilities.setCapability("recordVideo", recordVideo);
                capabilities.setCapability("recordScreenshots", recordScreenshots);
                //can add tags and build as capabilitiies also I believe - see sauce labs api documentation
                String sauceUrl = String.format("http://%s:%s...@ondemand.saucelabs.com:80/wd/hub", sauceUser, sauceKey);
                driver = new RemoteWebDriver(new URL(sauceUrl),capabilities);
                sessionId = ((RemoteWebDriver) driver) .getSessionId (). toString ();
                sauceClient = new SauceREST(sauceUser, sauceKey);
            }
            else if (host.equals("localhost")) {
                if (browser.equals("firefox")) {
                    driver = new FirefoxDriver();
                } else if (browser.equals("chrome")) {
                    System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/vendor/chromedriver.exe");
                    driver = new ChromeDriver();
                }
            }

            else if (host.equals("saucelabs-mobile")) {
                DesiredCapabilities capabilities = new DesiredCapabilities();
                capabilities.setCapability("browserName", browser);
                capabilities.setCapability("deviceName", device);
                capabilities.setCapability("platform", platform);
                capabilities.setCapability("platformVersion", platformVersion);
                capabilities.setCapability("appiumVersion", appiumVersion);
                capabilities.setCapability("device-orientation", deviceOrientation);
                             // Capabilities.setCapability ("browser version", browser version);
                capabilities.setCapability("name", testName);           //sets test name in saucelabs to the test name
                capabilities.setCapability("recordVideo", recordVideo);
                capabilities.setCapability("recordScreenshots", recordScreenshots);
                             //can add tags and build as capabilitiies
               // capabilities.setCapability("tags",tags);
                capabilities.setCapability("build", build);
                String sauceUrl = String.format("http://%s:%s...@ondemand.saucelabs.com:80/wd/hub", sauceUser, sauceKey);
                driver = new RemoteWebDriver(new URL(sauceUrl),capabilities);
                sessionId = ((RemoteWebDriver) driver) .getSessionId (). toString ();
                sauceClient = new SauceREST(sauceUser, sauceKey);
            }
        }

        @Override
        protected void after() {
            driver.quit();
        }
    };

        @Rule
        public TestRule watcher = new TestWatcher() {
            @Override
            protected void starting(Description description) {
                testName = description.getDisplayName();
            }

            @Override
            protected void failed(Throwable throwable, Description description) {
                if (host.contains("saucelabs")) {
                    sauceClient.jobFailed (sessionId);
                    System.out.println(String.format("https://saucelabs.com/tests/%s", sessionId));
                }
            }

            @Override
            protected void succeeded(Description description) {
                if (host.contains("saucelabs")) {
                    sauceClient.jobPassed (sessionId);
                }
            }
        };
}


----

package Tests;

import PageObjects.Login;
import org.junit.Before;
import org.junit.Test;
//import org.junit.experimental.categories.Category;
//import tests.groups.Smoke;

import static com.codeborne.selenide.Selenide.open;
import static org.junit.Assert.assertTrue;

public class TestLoginSimple extends Base {
    private Login login;
    private String title;

   @Before
   public void setUp() {
     //   login = new Login(driver);
    }

    @Test
   // @Category(Smoke.class)
    public void shouldSucceed() {
        login.with("tomsmith", "SuperSecretPassword!");
        login.successMessagePresent();                              //INFO: this calls Page object method $(By.id(".flash.success")).shouldBe(Condition.visible).shouldHave(Condition.text("some text"));

    }

    @Test
  //  @Category(Smoke.class)
    public void shouldFail() {
        login.with("tomsmith", "bad password");
        login.failureMessagePresent();
    }
}






Andrei Solntsev

unread,
Oct 5, 2015, 5:32:06 PM10/5/15
to Mark Winspear, selenide
Hi Mark!
It's easy. Just add one line in the end of before() method:

WebDriverRunner.setWebDriver(driver);

and one line in the beginning of after() method:
WebDriverRunner.setWebDriver(null);

It makes Selenide use your provided instance of webdriver.

2015-10-05 12:23 GMT+03:00 Mark Winspear <markwi...@gmail.com>:
Can anyone suggest how I can modify these tests to use Selenide in Sauce labs please?

Andrei Solntsev

Mark Winspear

unread,
Oct 6, 2015, 3:47:00 AM10/6/15
to selenide, markwi...@gmail.com
Thanks so much for the reply.  I've made the changes suggested, but unfortunately, I am still getting the same error as before when running...

java.lang.NoClassDefFoundError: org/json/JSONException
at Tests.Base$1.before(Base.java:43)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 22 more


java.lang.NullPointerException
at Tests.Base$2.failed(Base.java:94)
at org.junit.rules.TestWatcher.failedQuietly(TestWatcher.java:84)
at org.junit.rules.TestWatcher.access$300(TestWatcher.java:46)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:62)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)


java.lang.NullPointerException
at Tests.Base$2.failed(Base.java:94)
at org.junit.rules.TestWatcher.failedQuietly(TestWatcher.java:84)
at org.junit.rules.TestWatcher.access$300(TestWatcher.java:46)
at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:62)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

Mark Winspear

unread,
Oct 6, 2015, 4:01:46 AM10/6/15
to selenide
So sorry, that was my error. I now have it opening a browser locally but I am getting the following exception....

java.lang.NullPointerException
at java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1011)
at java.util.concurrent.ConcurrentHashMap.put(ConcurrentHashMap.java:1006)
at com.codeborne.selenide.impl.WebDriverThreadLocalContainer.setWebDriver(WebDriverThreadLocalContainer.java:65)
at com.codeborne.selenide.WebDriverRunner.setWebDriver(WebDriverRunner.java:94)
at Tests.Base$1.after(Base.java:82)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:50)

Andrei Solntsev

unread,
Oct 6, 2015, 8:34:09 AM10/6/15
to Mark Winspear, selenide
Sorry, my bad.
You don't need to call WebDriverRunner.setWebDriver(null).

2015-10-06 11:01 GMT+03:00 Mark Winspear <markwi...@gmail.com>:
WebDriverRunner.setWebDriver

Andrei Solntsev

Mark Winspear

unread,
Oct 6, 2015, 8:56:55 AM10/6/15
to selenide, markwi...@gmail.com
Perfect - thanks for your help!
Reply all
Reply to author
Forward
0 new messages