Docker Selenium Grid - Bug Reaper

                  Bug Reaper

Lean about Automation Testing,Selenium WebDriver,RestAssured,Appium,Jenkins,JAVA,API Automation,TestNG,Maven, Rest API, SOAP API,Linux,Maven,Security Testing,Interview Questions

Saturday, 21 March 2020

Docker Selenium Grid

Install Docker

Enable Virtualization  from Bios Setup


run VM from Oracle Box


Run Docker terminal


type --> docker info



Go to https://github.com/SeleniumHQ/docker-selenium


Pull all the images


docker pull selenium/hub






























docker pull selenium/node-chrome


docker pull selenium/node-firefox



docker images



docker logs hub


To See all containers


docker ps -a


Concept is we will have Hub Container and Nodes Container





So earlier biggest disadvantage of Using Grid earlier was we have to ask for extra infrastructure to Setup the VMs, but now we don't need those Extra Setup, with Docker Containers we can setup everything on or local machine on top of our Host OS


Now run all the images (after running these , they will become container)



docker run -d --name selenium -p 5900:5900 -p 4444:4444 selenium/standalone-chrome-debug

Note: 


5900 is VNC Port

we tell 5900:5900 which means map our internal port 5900 to 5900 of VNC viewer

The below command we will use to setup Selenium Hub as Docker Container


docker run -d -p 5000:4444 --name selenium-hub -P selenium/hub 


The below command we will use to setup Selenium Nodes as Docker Container
docker run -d --link selenium-hub:hub -P --name chrome selenium/node-chrome(Here we are starting node container and we are using link attribute to link it to the Hub container)


docker run -d --link selenium-hub:hub -P --name firefox selenium/node-firefox





Increase machines by following commands


docker run -d --link selenium-hub:hub selenium/node-firefox


docker run -d --link selenium-hub:hub selenium/node-chrome


Important commands

$ docker-machine ls


NAME   ACTIVE   DRIVER       STATE     URL

dev    *        virtualbox   Running   tcp://192.168.99.104:2376

$ docker-machine stop dev

$ docker-machine ls

NAME   ACTIVE   DRIVER       STATE     URL


dev    *        virtualbox   Stopped


We need to run Grid on Docker Machine IP

SO to find Ip of machine

docker-machine ip

here we have mapped port as 5000


http://192.168.99.100:5000/grid/console






To Stop and Remove all containers


docker stop $(docker ps -a -q)


docker rm $(docker ps -a -q)



Run Selenium tests in Docker


Make sure you have two firefox instances up


Example:

docker run -d --link selenium-hub:hub -P --name firefox selenium/node-firefox

docker run -d --link selenium-hub:hub selenium/node-firefox



// Here we are using URL of Docker where our Grid Console is present "http://192.168.99.100:5000/wd/hub"


driver = new RemoteWebDriver(new URL("http://192.168.99.100:5000/wd/hub"), capabilities);
We will be dividing test in two classes using testng.xml


First Class

package com.quikr.gridExample;

/** * Created by NEERAJ on 4/23/2017. */import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class GridExampleTest {

    public RemoteWebDriver driver;
    public static String appURL = "http://www.google.com";

    @BeforeClass    public void setUp() throws MalformedURLException {
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
// Here we are using URL of Docker  where our Grid Console is present "http://192.168.99.100:5000/wd/hub" 
      
driver = new RemoteWebDriver(new URL("http://192.168.99.100:5000/wd/hub"), capabilities);
        driver.manage().window().maximize();
    }

    @Test    public void testGooglePageTitleInIEBrowser() {
        System.out.println("*** Navigation to Application ***");
        driver.navigate().to(appURL);
        String strPageTitle = driver.getTitle();
        System.out.println("*** Verifying page title ***");
        Assert.assertTrue(strPageTitle.equalsIgnoreCase("Google"), "Page title doesn't match");
    }

    @AfterClass    public void closeBrowser() {
        if (driver != null) {
            driver.quit();
        }
    }
}

Second class
package com.quikr.gridExample;

/** * Created by NEERAJ on 4/23/2017. */
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;

public class GridExampleTest1 {

    public RemoteWebDriver driver;
    public static String appURL = "http://www.facebook.com";

    @BeforeClass    public void setUp() throws MalformedURLException {
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        driver = new RemoteWebDriver(new URL("http://192.168.99.100:5000/wd/hub"), capabilities);
        driver.manage().window().maximize();
    }

    @Test    public void testGooglePageTitleInIEBrowser() {
        System.out.println("*** One ***");
        driver.navigate().to(appURL);
        String strPageTitle = driver.getTitle();
        System.out.println("***  One Verifying page title ***");
        Assert.assertTrue(strPageTitle.equalsIgnoreCase("Facebook"), "Page title doesn't match");
    }

    @AfterClass    public void closeBrowser() {
        if (driver != null) {
            driver.quit();
        }
    }
}



Use this TestNg.xml file

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suite name="Main Test Suite" verbose="1" annotations="JDK" parallel="classes" thread-count="2">
    <test name="TestNG Grid">
        <packages>
            <package name="com.quikr.gridExample.*">
            </package>
        </packages>
    </test>

</suite>



No comments:

Post a Comment