Automation Using Selenium Webdriver

Monday, 7 November 2016

How To Set Proxy Settings In Selenium WebDriver Test

How To Set Proxy Settings In Selenium WebDriver Test
Sometimes, You need to set proxy settings of browser to run your selenium webdriver test.
 As you know, selenium launch fresh browser every time you run test so default proxy setting
will be No Proxy. You can set It In two ways.
1. Creating firefox profile and then use that profile In selenium test.
2. Using DesiredCapabilities. We will use DesiredCapabilities of selenium to set proxy.
What is DesiredCapabilities?
Using DesiredCapabilities, we can set and configure webdriver browser driver Instance
settings before launching It. Simplest example Is -> I wants to set proxy settings for
my webdriver browser Instance. I can do It using DesiredCapabilities.

How to set proxy settings of browser using DesiredCapabilities
I have created simple example to set proxy settings for firefox browser. It will set firefox driver
 browser Instance proxy settings as bellow.

HTTP Proxy = localhost, Port = 8080
SSL Proxy = localhost, Port = 8080
FTP Proxy = localhost, Port = 8080
SOCKS Host = localhost, Port = 8080

Example to set proxy for firefox driver Instance.
package Testing_Pack;

import java.io.IOException;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class ProxySettings {

 WebDriver driver;

 @BeforeTest
 public void setUpDriver() {
  //Set proxy IP and port. Here localhost Is proxy IP and 8080 Is Port number.
  //You can change both values as per your requirement.
  String PROXY = "localhost:8080";
  //Bellow given syntaxes will set browser proxy settings using DesiredCapabilities.
  Proxy proxy = new Proxy();
  proxy.setHttpProxy(PROXY).setFtpProxy(PROXY).setSslProxy(PROXY)
    .setSocksProxy(PROXY);
  DesiredCapabilities cap = new DesiredCapabilities();
  cap.setCapability(CapabilityType.PROXY, proxy);
  //Use Capabilities when launch browser driver Instance.
  driver = new FirefoxDriver(cap);
 }

 @Test
 public void start() throws IOException {
  System.out.println("Check your webdriver driver Instance's proxy setttings.");
 }
}

Run above example In your eclipse. It will open firefox browser driver Instance.
Check proxy settings for It from browser menu Tools -> Options -> Advanced tab -> Network tab -> Settings button. Click on Settings button. It will open connection settings popup as bellow.



You can see that proxy settings are set as given In test DesiredCapabilities configuration.
 You can change proxy IP and port number as per your requirement.


Maximize browser window using webdriver

Maximize browser window using webdriver


There are times when we want to to maximize the browser window during the execution of our script .For this purpose webdriver providers a built-in method and here is the syntax :

WebDriver driver;
driver.manage().window().maximize();

Here is the sample code using TestNG framework. :
import java.awt.Toolkit;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Reporter;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class MaximizeWindow {

WebDriver driver;

@BeforeTest
public void setUpDriver() {
driver = new FirefoxDriver();
      }
  

@Test
public void maximize() {
 //declare varibales for windows
org.openqa.selenium.Dimension defaultDim;
org.openqa.selenium.Dimension maximizeDim;
//Load google website on browser
driver.get("http://google.com");
//Display the current screen dimensions
defaultDim=driver.manage().window().getSize();
System.out.println("screenHeight before maximizing"+defaultDim.getHeight());
System.out.println("screenWidth before maximizing"+defaultDim.getWidth());
//maximize the window using webdriver method
driver.manage().window().maximize();
//Display the maximized window dimensions
maximizeDim=driver.manage().window().getSize();
System.out.println("screenHeight after maximizing:"+maximizeDim.getHeight());
System.out.println("screenWidth after maximizing:"+maximizeDim.getWidth());

      }


  }

Sunday, 6 November 2016

Advanced Real time Grouping Test Methods | TestNG Process

Test Methods are annotated with @Test;  These methods can be grouped and executed separately using TestNG framework.
The methods can also be executed based on the priority.

Please find a Test Suite with real-time implementation as given below,

public class classname{
private WebDriver driver;
private String baseUrl;

  @BeforeTest(groups = { "Group1", "Group2" })
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://www.justdial.com";
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  }

  @Test(groups = { "Group1" }, priority=3)
  public void test1() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("what")).sendKeys("G1a");
    driver.findElement(By.id("where")).sendKeys("Chennai");
    System.out.println("I am G1a");
  }

  @Test(groups = { "Group1" }, priority=2)
  public void test2() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("what")).sendKeys("G1b");
    driver.findElement(By.id("where")).sendKeys("Chennai");
    System.out.println("I am G1b");
  }

  @Test(groups = { "Group2" })
  public void test3() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("what")).sendKeys("G2a");
    driver.findElement(By.id("where")).sendKeys("Chennai");
    System.out.println("I am G2a");
  }

  @Test(groups = { "Group1" }, priority=1)
  public void test4() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("what")).sendKeys("G1c");
    driver.findElement(By.id("where")).sendKeys("Chennai");
    System.out.println("I am G1c");
  }

  @Test(groups = { "Group2" })
  public void test5() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("what")).sendKeys("G2b");
    driver.findElement(By.id("where")).sendKeys("Chennai");
    System.out.println("I am G2b");
  }

  @AfterTest(groups = { "Group1", "Group2" })
  public void tearDown() throws Exception {
    driver.quit();  
  }

Create .xml file and replace the code

Right-click on your Project/Class file > TestNG > "Convert to TestNG" > Replace with the xml code given below > Finish

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
<test name="Test1">
  <classes>
    <class name="package.classname"></class>
  </classes>
  <groups>
    <run>
      <include name="Group1"/>    
    </run>
  </groups>
</test>
</suite> <!-- Suite -->