Thursday, January 14, 2016

Selenium Using Browser and Profiles in RemoteWebDriver

Here I am using the Chrome browser through RemoteWebDriver. I am using the RemoteWebDriver constructor RemoteWebDriver(remoteAddress, desiredCapabilities).
For the Argument remoteAddress I am passing a String parameter using URL constructor and using desiredCapabilities method capability.setCapability(String key, Object value);.
Argument Key is String parameter Capability name and Object is ChromeOptions.
And you can use several ChromeOptions methods


      public static void chromeDriverRemote(RemoteWebDriver driver) {  
           // create a ChromeOptions object  
           ChromeOptions options = new ChromeOptions();  
           // adding a extension to the chrome  
           options.addExtensions(new File("C:\\DRIVE\\My Documents\\Downloads\\AdBlock_v2.46.crx"));  
           // loading a profile to the chrome  
           options.addArguments("user-data-dir=C:\\Users\\sudas\\AppData\\Local\\Google\\Chrome\\User Data\\sudas");  
           // maximize chrome  
           options.addArguments("start-maximized");  
           // Create a DesiredCapabilities object  
           DesiredCapabilities capability = new DesiredCapabilities();  
           // Use the DesiredCapabilities method  
           capability.setBrowserName("chrome");  
           // use the DesiredCapabilities method capability.setCapability(String key, Object value);  
           capability.setCapability(ChromeOptions.CAPABILITY, options);  
           try {  
                // STEP 01  
                driver = new RemoteWebDriver(new URL("http://10.129.63.183:4444/wd/hub"), capability);  
           } catch (MalformedURLException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  


Selenium using Forefox extension through RemoteWebDriver




Wednesday, January 13, 2016

Java System: Get Environment Map



  public static void getSystemEnvironmentMap() {
  Map envmap = System.getenv();
  Set> set = envmap.entrySet();
  Iterator> iterator = set.iterator();
  while (iterator.hasNext()) {

   Entry entry = iterator.next();
   System.out.println(entry.getKey());
   System.out.println(entry.getValue());
  }
 } 


This will return all environment variables




Selenium RemoteWebDriver usage

This is a write up about how to use RemoteWebDriver for all browsers

To start the server java -jar selenium-server-standalone-2.48.2.jar
When you start the server, chrome and firefox will work

Because the official documentation says

Client mode: where the language bindings connect to the remote instance. This is the way that the FirefoxDriver, OperaDriver and the RemoteWebDriver client normally work.

Server mode: where the language bindings are responsible for setting up the server, which the driver running in the browser can connect to. The ChromeDriver works in this way.

For internet Explorer and Chrome where language bindings are responsible for setting up the server start the server like



For Internet Explorer
C:\selenium>java -jar selenium-server-standalone-2.48.2.jar -Dwebdriver.ie.driver="C:\selenium\IEDriverServer_Win32_2.48.0\IEDriverServer.exe"

For Chrome
C:\selenium>java -jar selenium-server-standalone-2.48.2.jar -Dwebdriver.chrome.driver="C:\selenium\<path to chrome driver>

When you want to use multiple instance of same browser or multiple browser on same remote machine use GRID


Working with Internet Explorer 
_____________________________
Start the remote server using the following command

java -jar selenium-server-standalone-2.48.2.jar -Dwebdriver.ie.driver="C:\selenium\IEDriverServer_Win32_2.48.0\IEDriverServer.exe"

Below code will open ie browser at remote location  10.129.63.183:4444
desiredCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);  
is used to ignoreProtectedModeSettings for several zones in IE.

      public static void ieRemoteWebDriver(RemoteWebDriver rwd) {  
       DesiredCapabilities desiredCapabilities = DesiredCapabilities.internetExplorer();  
       desiredCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);  
       try {  
       rwd = new RemoteWebDriver(new URL("http://10.129.63.183:4444/wd/hub"), desiredCapabilities);  
       } catch (MalformedURLException e) {  
       // TODO Auto-generated catch block  
       e.printStackTrace();  
       }  
      }  


Below code will open ie browser at remote location  10.129.63.183:4444 
and will print browser details


      public static void ieRemoteWebDriver(RemoteWebDriver rwd) {  
           DesiredCapabilities desiredCapabilities = DesiredCapabilities.internetExplorer();  
           desiredCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,  
                     true);  
           try {  
                rwd = new RemoteWebDriver(new URL("http://10.129.63.183:4444/wd/hub"), desiredCapabilities);  
                Capabilities actualCapabilityes = rwd.getCapabilities();  
                System.out.println(actualCapabilityes.getVersion());  
                System.out.println(actualCapabilityes.getBrowserName());  
                System.out.println(actualCapabilityes.getPlatform());  
           } catch (MalformedURLException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
      }  


For setting Capability for browser selection . Common for all browsers

KeyTypeDescription
browserNamestringThe name of the browser being used; should be one of {android|chrome|firefox|htmlunit|internet explorer|iPhone|iPad|opera|safari}.
versionstringThe browser version, or the empty string if unknown.
platformstringA key specifying which platform the browser should be running on. This value should be one of {WINDOWS|XP|VISTA|MAC|LINUX|UNIX|ANDROID}. When requesting a new session, the client may specify ANY to indicate any available platform may be used. For more information see [GridPlatforms]

Setting desired capability for browser name

DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setBrowserName("internet explorer");
desiredCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);

OR
   DesiredCapabilities desiredCapabilities = DesiredCapabilities.internetExplorer();  
           desiredCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,  
                     true);  



Setting desired capability for Version, Platform (example)
desiredCapabilities.setCapability("platform", Platform.ANY);

desiredCapabilities.setCapability("version", "11");


  • For a list of capability specific to Internet Explorer please refer HERE




Working with Chrome Driver 
_____________________________

Start the server in remote machine

java -jar selenium-server-standalone-2.48.2.jar -Dwebdriver.chrome.driver="C:\selenium\chromedriver_win32\chromedriver.exe"



Selenium Chrome Driver Usage

Available constructors for Chrome Driver




A list of all Chrome Driver Capability

https://sites.google.com/a/chromium.org/chromedriver/capabilities

Chrome Driver usage for constructor ChromeDriver(service, options);

Opening a already created Chrome profile to a new Chrome browser instance.
To create a new chrome profile, enter chrome://version/ in chrome navigation bar and checkout the profilePath. Close the browser and rename the default profile path.

Now if you open the Chrome browser the browser will create a new default profile folder Default again
OR
you can use the below method and start an instance of Chrome browser by setting
options.addArgument("user-data-directory=C:/xx/xx/profile_name")

          // USE OF CHROME Driver constructor ChromeDriver(service, options)
 public static void ChromeLocalDriver02(WebDriver driver) {
  System.setProperty("webdriver.chrome.driver", "C:\\Java_Source_Code\\chromedriver_win32\\chromedriver.exe");
  // setting up the service
  ChromeDriverService.Builder builder = new ChromeDriverService.Builder();
  ChromeDriverService service = builder.withLogFile(new File("C:\\Test\\ie_log\\chrome1.log")).build();
  // chrome options
  ChromeOptions options = new ChromeOptions();
  // Opening Chrome browser with current default profile
  options.addArguments("user-data-dir=C:\\Users\\sudas\\AppData\\Local\\Google\\Chrome\\User Data\\sudas");
  // this is to start Chrome windows as maximized
  options.addArguments("start-maximized");
  driver = new ChromeDriver(service, options);
 } 
      }  


To find out which profile the browser is currently using enter chrome://version/ in Chrome's address bar and checkout the "ProfilePath"

Chrome Driver usage for constructor ChromeDriver(service, capabilities);


      // USE OF CHROME Driver CAPABILITY through DesiredCapabilities  
      // Used constructor ChromeDriver(service, capabilities)  
      public static void ChromeLocalDriver01(WebDriver driver) {  
           System.setProperty("webdriver.chrome.driver", "C:\\Java_Source_Code\\chromedriver_win32\\chromedriver.exe");  
           // "ChromeDriverService" similar to InternetExplorerDriverService  
           ChromeDriverService.Builder builder = new ChromeDriverService.Builder();  
           ChromeDriverService service = builder.withLogFile(new File("C:\\Test\\ie_log\\chrome1.log")).build();  
           DesiredCapabilities capabilities = new DesiredCapabilities();  
           // capabilities.setCapability(ChromeDriverService.CHROME_DRIVER_VERBOSE_LOG_PROPERTY,  
           // true);  
           capabilities.setCapability(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, true);  
           driver = new ChromeDriver(service, capabilities);  
      }  

Chrome Driver usage for constructor ChromeDriver(capabilities);

      public static void ChromeLocalDriver03(WebDriver driver) {  
           System.setProperty("webdriver.chrome.driver", "C:\\Java_Source_Code\\chromedriver_win32\\chromedriver.exe");  
           ChromeOptions options = new ChromeOptions();  
           options.addExtensions(new File("C:\\DRIVE\\My Documents\\Downloads\\AdBlock_v2.46.crx"));  
           DesiredCapabilities capabilities = new DesiredCapabilities();  
           capabilities.setCapability(ChromeOptions.CAPABILITY, options);  
           driver = new ChromeDriver(capabilities);  
      }  

Monday, January 11, 2016

Java Singletone class


For a singleton class it allows to create only one object of the class



 package singelton;  
 /***  
  * Steps to create a Singleton class Create a standard java class Create an  
  * instance of the same class like private static SingleTone instance = new SingleTone();  
  * create a private default constructor  
  * create a method and return the instance  
  *   
  * @author sudas  
  *  
  */  
 public class SingleTone {  
      public static void main(String[] args) {  
 SingleTone obj1 = SingleTone.singleInstance();  
 SingleTone obj2 = SingleTone.singleInstance();  
 System.out.println(obj1);  
 System.out.println(obj2);  
      }  
      private static SingleTone instance = new SingleTone();  
      private SingleTone() {  
           // TODO Auto-generated constructor stub  
      }  
      public static SingleTone singleInstance() {  
           return instance;  
      }  
 }  

When you run this you will see both the objects (obj1 and obj2) are pointing to the same memory on hip, so basically obj1 and obj2 both are same objects of class Singleton


 singelton.SingleTone@2a139a55  
 singelton.SingleTone@2a139a55  

Saturday, January 9, 2016

Selenium: WebDriverEventListener and EventFiringWebDriver

WebDriverEvenListner interface and EventFiringWebDriver class


When you are running test using WebDriver Interface in Selenium there are many events that get fired before and after navigation to an URL.

EventFiringWebDriver class is a wrapper for normal WebDriver interface, that gives the capability to WebDriver to fire Events. EventListener class (let’s say) which implements WebDriverEventListner handles all the events that are dispatched by EventFiringWebDriver class

To capture even there are two different type of implementations
  1. By creating a class say EventListner implements WebDriverEventListner
OR

    2. By creating a class say EventListner extends AbstractWebDriverEventListner


class EventListner implements WebDriverEventListener
{
@Overriding method 
   ////////// NAVIGATION RELATED METHODS ////////////////
   /////////////////// FINDBY RELATED METHODS ///////////////
   //////////////////// CLICKON RELATED METHODS ///////////////
   ///////////////// CHANGE OF VALUE RELATED METHODS //////////////
   /////////////// SCRIPT EXECUTION RELATED METHODS ///////////////
   /////////////// EXCEPTION RELATED METHODS ///////////////////////


}



/***
* Step -02 Register the WebDriverEventListner (Listener) with the WebDriver
* instance. 1. Create WebDriver instance 2. Create EventFiringWebDriver
* instance and pass the WebDriver object to EventFiringWebDriver
* constructor argument. 3. Create an instance of EventListener.
* EventListner class created at step-01 4. Register the listener i.e
* EventFiringWebDriver efwd = new EventFiringWebDriver(driver)
* efwd.register(object of EventListner)
*/


public static void registerListener(WebDriver driver) {
// 1. Create WebDriver instance
driver = new ChromeDriver();

// * 2. Create EventFiringWebDriver instance and pass the WebDriver
// object to EventFiringWebDriver constructor argument.
EventFiringWebDriver efwd = new EventFiringWebDriver(driver);

// * 3. Create an instance of EventListener. EventListner class created
// at step-01
EventListner listner = new EventListner();

// * 4. Register the listener
efwd.register(listner);

}