Saturday 23 August 2014

Selenium Grid code for test in different Browser and OS


Selenium Grid code for test in different Browser and OS
Selenium Grid is a part of the Selenium Suite that specializes on running multiple tests across different browsers, operating systems, and machines in parallel.
Selenium Grid has 2 versions - the older Grid 1 and the newer Grid 2. We will only focus on Grid 2 because Grid 1 is gradually being deprecated by the Selenium Team. 

When to Use Selenium Grid?

You should use Selenium Grid when you want to do either one or both of following :
  • Run your tests against different browsers, operating systems, and machines all at the same time.This will ensure that the application you are testing is fully compatible with a wide range of browser-OS combinations.
  • Save time in execution of your test suites. If you set up Selenium Grid to run, say, 4 tests at a time, then you would be able to finish the whole suite around 4 times faster.

import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class SelumnGridTest
{
    WebDriver driver;
    String baseUrl, nodeURL;
    @BeforeTest
    public void setup() throws MalformedURLException
    {
        baseUrl="http://newtours.demoaut.com/";
        nodeURL="http://172.16.2.19:5566/wd/hub";
        DesiredCapabilities capabilities = DesiredCapabilities.firefox();
        capabilities.setBrowserName("firefox");
        capabilities.setPlatform(Platform.XP);
        driver=new  RemoteWebDriver(new URL(nodeURL),capabilities);
      
    }
    @AfterTest
    public void aftertest()
    {
        driver.quit();
    }
    @Test
    public void simpletest()
    {
        driver.get(baseUrl);
        Assert.assertEquals(driver.getTitle(),driver.getTitle());
    }
    }

Use- selenium-server-standalone-2.42.0 jar file
Configure HUB:- In CMD
D:\Automation_Testing\selenium-server-2.35.0\selenium-2.35.0>java -jar selenium-server-standalone-2.42.0.jar -role hub

Configure Node- IN CMD-provide ip address in your systems
D:\Automation_Testing\selenium-server-2.35.0\selenium-2.35.0>java -jar selenium-server-standalone-2.42.0.jar -role webdriver -hub http://172.16.2.19:4444/grid/register -port 5566

Browser instance like max

java -jar selenium-server-standalone-<version>.jar -role node -hub http://localhost:4444/grid/register -nodeConfig defaultNodeConfig.json 

java -jar selenium-server-standalone-<version>.jar -role node -hub http://localhost:4444/grid/register -nodeConfig defaultNodeConfig.json - See more at: http://selenium.polteq.com/en/launch-selenium-node-with-a-json-configuration-file/#sthash.FhfTzvQX.dpuf
webdriver-hub 

http://172.16.2.19:4444/grid/register -port 5566

Dynamic Alert handling code:-

 if(wait.until(ExpectedConditions.alertIsPresent())==null){
         
     }else{
         action.alertAccept();
     }
     if(wait.until(ExpectedConditions.alertIsPresent())==null){
        
     }else{
         action.alertAccept();
         }
     if(wait.until(ExpectedConditions.alertIsPresent())==null){
        
     }else{
         action.alertAccept();
         }
Tags:-user extensions Verification Version1 webdriver.chrome.driver with Firefox with Google Chrome with java with jquery XML Annotation Assertion basics ChromeDriver chromedriver.exe Drag and Drop eclipse findElements firefox flash FLASH (FLEX) getAttribute() getProfile getText() iframe implicitlyWait InternetExplorerDriver java applet JavascriptExecutor Junit framework learn by example list Locating by XPath Locators Parameterized Test ProfilesIni regular expression ScreenShot Selenium Selenium / WebDriver API Selenium 2.0 Selenium IDE Selenium Webdriver techno Fun TestNG TestNG framework