Showing posts with label selenium. Show all posts
Showing posts with label selenium. Show all posts

Wednesday, November 29, 2017

Calendar Control / DatePicker Automation with Selenium Webdriver

Selenium Automation for AJAX Calendar Control

There are different types of calendar controls in web as

  1. Ajax Calendar Control
  2. JQuery Calendar ( DatePicker ) control
  3. BootStrap Calendar control
There is no built in selenium automation support for any of above calendar control. We you need to write a logic to handle all case in selecting a date.

Here, I am going to discuss the approach on how to automate AJAX Calendar Control

Below image explains the locator for different controls (elements) in calendar.

Create Utility Page Class for Calendar Control

Now just use above class in test case.

That's All.



Sunday, May 14, 2017

Tutorial : Setting up Selenium GRID in LAN

Trying to learn and set up selenium GRID in local network? Here is the simple tutorial to start with.

Selenium GRID - components

  1. Selenium GRID Hub
  2. Selenium GRID Node

Step 1: Start Selenium GRID HUB Server 

java  -jar selenium-server-standalone-3.3.1.jar -role hub

After running this command you will a message as  below

16:55:49.766 INFO - Selenium Grid hub is up and running
17:16:34.636 INFO - Nodes should register to http://192.168.0.128:4444/grid/register/

Step 2: Start Selenium GRID NODE

2.1 Go to node machine & node configuration file with below JSON

FileName: nodeConfiguration.json


   "capabilities":[ 
      { 
         "browserName":"chrome",
         "maxInstances":10,
         "platform":"WIN8",
         "version":"58",
         "seleniumProtocol":"WebDriver"
      },
      {
         "browserName":"firefox",
         "maxInstances":3,
         "platform":"WIN8",
         "version":"53",
         "seleniumProtocol":"WebDriver"
      }
   ],
   "debug":false,
   "proxy":"org.openqa.grid.selenium.proxy.DefaultRemoteProxy",
   "register":true,
   "registerCycle":5000,
   "maxSession":6
}

 2.2 Execute below command to start node and connect to hub

java -Dwebdriver.chrome.driver=C:\Users\vikas\selenium_drivers\chromedriver.exe -Dwebdriver.gecko.driver=c:\geckodriver.exe -jar selenium-server-standalone-3.3.1.jar -role node -nodeConfig nodeConfigFile.json -hub http://192.168.0.128:4444/grid/register

Step 3: Confirm Node is connected and hub is running by opening below url in browser

http://192.168.0.128:4444/grid/console

Step 4: Run Test case from eclipse. below is the sample code.

package com.vikas.samples;

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

import org.apache.tools.ant.taskdefs.XSLTProcess.TraceConfiguration;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.vikas.framework.core.Browser;

public class GridExecutionDemo {

    @Test(dataProvider = "multipleTests")
    public void testOnRemmoteChromeBrowser(int testNumber, String browserName) throws MalformedURLException {
      
        // WebDriver driver = new ChromeDriver();
        System.out.println("Starting test :" + testNumber);
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setBrowserName(browserName);
        caps.setPlatform(Platform.WIN8);
        // OR caps.setCapability("browserName", "chrome");
        URL hubUrl = new URL("http://192.168.0.128:4444/wd/hub");

        WebDriver driver = new RemoteWebDriver(hubUrl, caps);
        driver.get("http://google.com");
        driver.findElement(By.name("q")).sendKeys("Vikas Thange Selenium");

        driver.quit();
    }

    @DataProvider(parallel = true)
    public Object[][] multipleTests() {
        return new Object[][] { { 1, "firefox"}, { 2, "chrome" } };
    }
}

Tuesday, October 25, 2016

Opening url in new Window?


How to open a new window with a new URL while browser automation session is ongoing?


Have you encountered situation where you are automating a website and same time you need to open other browser window to do something like read OTP or read E-Mail and come back to the AUT window and proceed automation...

I am sure If you are working in web automation testing you might have encountered this.

What solution we have ?
  1. Launch a new WebDriver instance while one is having AUT loaded.
    • For example below code :
      // Initial WebDriver instance to perform Automation Testing on AUT
      // (if Selenium 3.0 then set webdriver.gecko.driver path)
      WebDriver driver = new FirefoxDriver();
      driver.get("http://myAUT.com");
      // TODO: perform some automation stuff
      // ....
      // Now I want to open gmail in new window without disturbing
      // myAUT.com site
      // So launch a new WebDriver instance 
      WebDriver gmailBrowser = new FirefoxDriver();
      gmailBrowser.get("http://www.gmail.com");
      // Perform operations like read mail data here
      // String otp = gmailDriver.findElement(By.xpath("some locator"))
      // .getText(); 
      // Now close browser and go back to myAUT.com
      gmailBrowser.close();
      // now type otp in myAUT.com
      driver.findElement(By.id("some id")).sendKeys(otp);
      

    • But is this really good approach? NO!!! absolutely not! 
    • Because it's more confusing to work on two webdriver instance at a time.
    • Both WebDriver instance do not share common session with server.
  2. Launch a new url in new tab of existing WebDriver session
    • Why to start a new WebDriver instance when you do use already existing one?
    • This will also share existing session with server
    • Approach: - 
      • Execute a JavaScript code it will open a new url in new tab/window but in existing WebDriver instance
      • Switch driver to point newly opened driver and do some task
      • Once you are done close newly opened tab/window and switch back to AUT browser window
    • Below is the sample script
      WebDriver driver = new FirefoxDriver();
      WebDriver driver = new FirefoxDriver();
      driver.get("http://google.com");
      // copy current window handle for future reference
      String autWindowHandle = driver.getWindowHandle();
      JavascriptExecutor executor = (JavascriptExecutor) driver;
      executor.executeScript("window.open('http://www.gmail.com','_blank')");
      
      // Switch driver to gmail and do your work
      for (String handle : driver.getWindowHandles()) {
       driver.switchTo().window(handle);
      }
      // do operations and read data.
      // String otp = driver.findElement(By.id("some id")).getText();
      
      //Now close gmail and switch back to AUT site
      driver.close();
      driver.switchTo().window(autWindowHandle);
      //Continue automation testing
      
I hope this will be useful to solve your problem.