Monday, 18 April 2016

Selenium Question

-          Assume, given web application contains 10 dynamic text boxes.
-          Text box placing/location will also changes dynamically.
-          Now we need to key in text in one specific text box, which does not contain any unique locator ( We are not able to find out uniqueness with partial text too )

How you do you achieve this?                                                  

Analysis:
-          No unique ID , tool tip, value, css style, etc.
-          We are not able to draw unique ness with partial ID / text / value / etc

Tip:
-          Only one control is having unique ID and whose position is dynamic.
      

  Idea:
-          Can we use below Xpath selectors


AxisName
Result
ancestor
Selects all ancestors (parent, grandparent, etc.) of the current node
ancestor-or-self
Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself
attribute
Selects all attributes of the current node
child
Selects all children of the current node
descendant
Selects all descendants (children, grandchildren, etc.) of the current node
descendant-or-self
Selects all descendants (children, grandchildren, etc.) of the current node and the current node itself
following
Selects everything in the document after the closing tag of the current node
following-sibling
Selects all siblings after the current node
namespace
Selects all namespace nodes of the current node
parent
Selects the parent of the current node
preceding
Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes and namespace nodes
preceding-sibling
Selects all siblings before the current node
self
Selects the current node


Examples:

Example
Result
child::book
Selects all book nodes that are children of the current node
attribute::lang
Selects the lang attribute of the current node
child::*
Selects all element children of the current node
attribute::*
Selects all attributes of the current node
child::text()
Selects all text node children of the current node
child::node()
Selects all children of the current node
descendant::book
Selects all book descendants of the current node
ancestor::book
Selects all book ancestors of the current node
ancestor-or-self::book
Selects all book ancestors of the current node - and the current as well if it is a book node
child::*/child::price
Selects all price grandchildren of the current node




Examples:


//td[text() = ' Color Digest ']/following-sibling::td[2]
//h2[contains(text(),'Hello')]/parent::div//div[//a[text()='SELENIUM']]/following-sibling::div[@class='rt-grid-2']

Selenium Question


-          Assume, given web application is accessible in one Machine ( IP : 10.10.10.10)
-          You need to write selenium tests and run.
-          You are not allowed to install / download any software except browser in IP: 10.10.10.10.
How you do you achieve this?                                                 
Analysis:
-          We can start coding in our local laptop , later we can think about executing tests.
-          To find out locators ( xpaths / css / etc) , connect to the machine IP 10.10.10.10 via remote desktop and fetch as much info as needed.
Now Execution part,
-          When we execute our test in local, tests should run in the IP 10.10.10.10.
-          This can be achieved via selenium GRID.
-          Create a node in IP 10.10.10.10 and connect to a HUB ( either in your laptop / IP 10.10.10.10)
-          Write Remote web driver code instead of plain webdriver.

Just hit run. That’s all.

Selenium Grid (Advanced)

Selenium Grid distributes our tests across multiple machines so that we can run them in parallel, cutting down the time required for running tests.

The selenium-server-standalone package includes the Hub, WebDriver, and Selenium RC .

Setting up Hub: (Simple)
java -jar selenium-server-standalone-2.53.0.jar -port 4444 -role hub

Setting up Node:(Simple)
java -jar selenium-server-standalone-2.53.0.jar -role webdriver -hub http://xx.xx.xx.xx:4444/grid/register –port 8989

Setting up Node:(Complex)
If you want to setup a node with only IE browsers

Option1: via Command Text
java -jar selenium-server-standalone-2.53.0.jar -role webdriver -browser "browserName=internet explorer,version=11,maxinstance=1,platform=WINDOWS" -hub http://xx.xx.xx.xx:4444/grid/register –port 8989

Option2: via Json file

Save below Json as selenium-node-win-ie11-cfg.json

{
           "class": "org.openqa.grid.common.RegistrationRequest",

           "capabilities":
                              [
                                 { "seleniumProtocol": "WebDriver", 
                                    "browserName": "internet explorer",
                                    "version": "11",
                                    "maxInstances": 1,
                                    "platform" : "WINDOWS"
                                 }
                              ],

          "configuration":
                      {
                           "port": 8989,
                           "register": true,
                           "host": "xx.xx.xx.100", 
                           "proxy": "org.openqa.grid.selenium.proxy. DefaultRemoteProxy", 
                           "maxSession": 2,
                          "hubHost": "xx.xx.xx.100",
                          "role": "webdriver",
                          "registerCycle": 5000,
                          "hub": "http://xx.xx.xx.100:4444/grid/register",
                          "hubPort": 4444,
                          "remoteHost": "http://xx.xx.xx.101:8989"
                       }
}


one more json:


{
  "capabilities":
      [
        {
          "browserName": "chrome",
          "maxInstances": 5
        },
        {
          "browserName": "firefox",
          "maxInstances": 5
        },
        {
          "browserName": "internet explorer",
          "maxInstances": 5
        },
        {
          "browserName": "safari",
          "maxInstances": 5
        }
      ],
    "configuration":
        {
"_comment": "Configuration for Node",
        "nodeTimeout":120,
        "port":5555,
        "hubPort":4444,
        "hubHost":"hubIpAddress",
        "nodePolling":2000,
        "registerCycle":10000,
        "register":true,
        "cleanUpCycle":2000,
        "timeout":30000,
        "maxSession":5,
        }
}


Command Text:

java -jar selenium-server-standalone-53.jar -role webdriver -nodeConfig selenium-node-win-ie11-cfg.json


Selenium Remote WebDriver code:

DesiredCapabilities cap = new DesiredCapabilities();  
                                cap.setBrowserName("firefox");
                                cap.setPlatform(org.openqa.selenium.Platform.WINDOWS);


WebDriver driver = new RemoteWebDriver(new URL("http://xx.xx.xx.xx:4444/wd/hub"),cap);



Suite.
xml:

<suite name="Parallel Tests" verbose="1" thread-count="4"
parallel="tests">

</suite>

thread-count=“4” describes the maximum number of threads to be executed in parallel.


---------------------------------------------------------------------------------------------------------------------------------

Each node contains  
5 Chrome, 5 Firefox and 1 IE browser under Browser section like below.




Do you want only one IE browser?

java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
http://localhost:4444/grid/register -port 5556 -browser browserName=iexplore




You want one browser per each type?

java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub
http://localhost:4444/grid/register -port 5556 -browser browserName=iexplore
-browser browserName=firefox -browser browserName=chrome




maxInstances:

maxInstance is used to limit the number of browser initialization in a node.
For example if you want to work with 2 Firefox and 2 IE then you can start the node using maxInstance.
java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub 
http://localhost:4444/grid/register -port 5556 -browser browserName=firefox,maxInstance=3


maxSession:
maxSession is used to configure how many number of browsers can be used parallel in the remote system.
java -jar selenium-server-standalone-2.41.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=chrome,maxInstance=3 -browser browserName=firefox,maxInstance=3 –maxSession 3



Selenium Grid ( Simple )

Selenium Grid distributes our tests across multiple machines so that we can run them in parallel, cutting down the time required for running tests.

The selenium-server-standalone package includes the Hub, WebDriver, and Selenium RC needed to run the grid.

Setting up Hub: (Simple)
java -jar selenium-server-standalone-2.53.0.jar -port 4444 -role hub

Setting up Node:(Simple)
java -jar selenium-server-standalone-2.53.0.jar -role webdriver -hub http://xx.xx.xx.xx:4444/grid/register –port 8989 -Dwebdriver.chrome.driver=path/to/chromedriver.exe


Selenium Remote WebDriver code:

DesiredCapabilities cap = new DesiredCapabilities(); 
                                cap.setBrowserName("firefox");
                                cap.setPlatform(org.openqa.selenium.Platform.WINDOWS);


WebDriver driver = new RemoteWebDriver(new URL("http://xx.xx.xx.xx:4444/wd/hub"),cap);


Thats all.

Design Patterns : singleton / Single Object class


Here is the simple definition.

Single Object class have its constructor as private and have a static instance of itself.

Explanation:

1. Private constructor:  This stops us creating object for the class, Not even one object creation is allowed.

But, we want one object.
How can we create it?

2. Static instance of itself: Create a instance of self and mark it as static.

'Static' help us to access the instance with out creating object ( Anyways with the point#1, we can not create instance )


public class Singleton {

   private static Singleton singleton = new Singleton( );
   
   /* A private Constructor prevents any other 
    * class from instantiating.
    */
   private Singleton(){ }
   
   /* Static 'instance' method */
   public static Singleton getInstance( ) {
      return singleton;
   }
    
}

// File Name: SingletonDemo.java
public class SingletonDemo {
   public static void main(String[] args) {
      Singleton tmp = Singleton.getInstance( );
      tmp.demoMethod( );
   }
}