Showing posts with label Grid. Show all posts
Showing posts with label Grid. Show all posts

Friday, 15 July 2016

Selenium Remote WebDriver + Sauce Labs

Sauce Labs is a Monster Grid to run our tests remotely.

The Sauce Labs cloud-based testing platform helps you run your SeleniumWebdriver tests on the cloud, test web and mobile apps immediately on 350+ browsers and platforms, and easily debug your code with the help of videos and screenshots.

Lest quickly setup the things.


Step 1: Create a sauce labs account: https://saucelabs.com/home

Choose Free Trail, fill the info and then click on Create Account button.



Then verify your account by clicking link in the Email.

Now, you can see Dashboard screen.



Observe, Bottom left corner where you can see your account Firstname and Last name.

Click on it,



We need UserName and AccessKey to connect to sauce labs from our selenium code.



Step 2: Write Remote WebDriver code:

Here is the sample code.

package com.sauceLabs.automation;

import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

public class Test {

       public static final String USERNAME = "kXXXXXX";
       public static final String AUTOMATE_KEY = "d87XXXX-XXX-XXX-XXX-XXX";
       public static final String URL = "https://" + USERNAME + ":" + AUTOMATE_KEY + "@ondemand.saucelabs.com:80/wd/hub";

       public static void main(String[] args) throws Exception {

              DesiredCapabilities caps = new DesiredCapabilities();
              caps.setCapability("browser", "IE");
              caps.setCapability("browser_version", "7.0");
              caps.setCapability("os", "Windows");
              caps.setCapability("os_version", "XP");
              caps.setCapability("browserstack.debug", "true");

              WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
              driver.get("http://www.google.com");
              WebElement element = driver.findElement(By.name("q"));

              element.sendKeys("BrowserStack");
              element.submit();

              System.out.println(driver.getTitle());
              driver.quit();

       }

}


Just run it, That’s all.







Parallel Testing thumb rules:

Parallel Testing thumb rules:

Each Test must be atomic, must have no dependency.

Implement Grouping mechanism to run in small batches.

Write meaningful comments for each test.


Choose a reliable GRID environment.

Monday, 18 April 2016

Selenium Question


-          Assume, given web application is accessible in one Machine ( IP : 10.10.10.10)
-          You need to write selenium tests and run.
-          You are not allowed to install / download any software except browser in IP: 10.10.10.10.
How you do you achieve this?                                                 
Analysis:
-          We can start coding in our local laptop , later we can think about executing tests.
-          To find out locators ( xpaths / css / etc) , connect to the machine IP 10.10.10.10 via remote desktop and fetch as much info as needed.
Now Execution part,
-          When we execute our test in local, tests should run in the IP 10.10.10.10.
-          This can be achieved via selenium GRID.
-          Create a node in IP 10.10.10.10 and connect to a HUB ( either in your laptop / IP 10.10.10.10)
-          Write Remote web driver code instead of plain webdriver.

Just hit run. That’s all.

Selenium Grid (Advanced)

Selenium Grid distributes our tests across multiple machines so that we can run them in parallel, cutting down the time required for running tests.

The selenium-server-standalone package includes the Hub, WebDriver, and Selenium RC .

Setting up Hub: (Simple)
java -jar selenium-server-standalone-2.53.0.jar -port 4444 -role hub

Setting up Node:(Simple)
java -jar selenium-server-standalone-2.53.0.jar -role webdriver -hub http://xx.xx.xx.xx:4444/grid/register –port 8989

Setting up Node:(Complex)
If you want to setup a node with only IE browsers

Option1: via Command Text
java -jar selenium-server-standalone-2.53.0.jar -role webdriver -browser "browserName=internet explorer,version=11,maxinstance=1,platform=WINDOWS" -hub http://xx.xx.xx.xx:4444/grid/register –port 8989

Option2: via Json file

Save below Json as selenium-node-win-ie11-cfg.json

{
           "class": "org.openqa.grid.common.RegistrationRequest",

           "capabilities":
                              [
                                 { "seleniumProtocol": "WebDriver", 
                                    "browserName": "internet explorer",
                                    "version": "11",
                                    "maxInstances": 1,
                                    "platform" : "WINDOWS"
                                 }
                              ],

          "configuration":
                      {
                           "port": 8989,
                           "register": true,
                           "host": "xx.xx.xx.100", 
                           "proxy": "org.openqa.grid.selenium.proxy. DefaultRemoteProxy", 
                           "maxSession": 2,
                          "hubHost": "xx.xx.xx.100",
                          "role": "webdriver",
                          "registerCycle": 5000,
                          "hub": "http://xx.xx.xx.100:4444/grid/register",
                          "hubPort": 4444,
                          "remoteHost": "http://xx.xx.xx.101:8989"
                       }
}


one more json:


{
  "capabilities":
      [
        {
          "browserName": "chrome",
          "maxInstances": 5
        },
        {
          "browserName": "firefox",
          "maxInstances": 5
        },
        {
          "browserName": "internet explorer",
          "maxInstances": 5
        },
        {
          "browserName": "safari",
          "maxInstances": 5
        }
      ],
    "configuration":
        {
"_comment": "Configuration for Node",
        "nodeTimeout":120,
        "port":5555,
        "hubPort":4444,
        "hubHost":"hubIpAddress",
        "nodePolling":2000,
        "registerCycle":10000,
        "register":true,
        "cleanUpCycle":2000,
        "timeout":30000,
        "maxSession":5,
        }
}


Command Text:

java -jar selenium-server-standalone-53.jar -role webdriver -nodeConfig selenium-node-win-ie11-cfg.json


Selenium Remote WebDriver code:

DesiredCapabilities cap = new DesiredCapabilities();  
                                cap.setBrowserName("firefox");
                                cap.setPlatform(org.openqa.selenium.Platform.WINDOWS);


WebDriver driver = new RemoteWebDriver(new URL("http://xx.xx.xx.xx:4444/wd/hub"),cap);



Suite.
xml:

<suite name="Parallel Tests" verbose="1" thread-count="4"
parallel="tests">

</suite>

thread-count=“4” describes the maximum number of threads to be executed in parallel.


---------------------------------------------------------------------------------------------------------------------------------

Each node contains  
5 Chrome, 5 Firefox and 1 IE browser under Browser section like below.




Do you want only one IE browser?

java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
http://localhost:4444/grid/register -port 5556 -browser browserName=iexplore




You want one browser per each type?

java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
http://localhost:4444/grid/register -port 5556 -browser browserName=iexplore
-browser browserName=firefox -browser browserName=chrome




maxInstances:

maxInstance is used to limit the number of browser initialization in a node.
For example if you want to work with 2 Firefox and 2 IE then you can start the node using maxInstance.
java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub 
http://localhost:4444/grid/register -port 5556 -browser browserName=firefox,maxInstance=3


maxSession:
maxSession is used to configure how many number of browsers can be used parallel in the remote system.
java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=chrome,maxInstance=3 -browser browserName=firefox,maxInstance=3 –maxSession 3



Selenium Grid ( Simple )

Selenium Grid distributes our tests across multiple machines so that we can run them in parallel, cutting down the time required for running tests.

The selenium-server-standalone package includes the Hub, WebDriver, and Selenium RC needed to run the grid.

Setting up Hub: (Simple)
java -jar selenium-server-standalone-2.53.0.jar -port 4444 -role hub

Setting up Node:(Simple)
java -jar selenium-server-standalone-2.53.0.jar -role webdriver -hub http://xx.xx.xx.xx:4444/grid/register –port 8989 -Dwebdriver.chrome.driver=path/to/chromedriver.exe


Selenium Remote WebDriver code:

DesiredCapabilities cap = new DesiredCapabilities(); 
                                cap.setBrowserName("firefox");
                                cap.setPlatform(org.openqa.selenium.Platform.WINDOWS);


WebDriver driver = new RemoteWebDriver(new URL("http://xx.xx.xx.xx:4444/wd/hub"),cap);


Thats all.