Friday 23 February 2018

Selenium Wrapper Automation

Selenium Wrapper automation is not any automation technique or framework it is just strategy which used in creation of automation framework.

Lets understand selenium wrapper automation with example.

when we are going to click any element using automation then we have to first check weather that element is present on page or visible or not. Once element is visible on page then after we can click on that element.
'Click on Element' event is common event in any automation.

Now assume that you have a scenario for automation and in that scenario you have to perform 5 or more time use 'Click on element' event. So, every time you have to first check weather element is present or not otherwise selenium will throw 'Element Not Found' exception.

So, rather than checking every time element present or not and wait till element visible we can create one function which perform both the thing on once go.
1. Check Element visible or not and if not then wait till some specific time
2. Click on element once it is visible

Thats called selenium wrapper automation.

It is so easy!! right?

you can also do same thing for many automation events which is common like drop down events, element visible or not etc.

You can use below function for click event into your automation framework.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public boolean clickOnElement(WebDriver driver, WebElement vElement)
    {
     boolean bClickOnElement = true;

        final int WAIT_TIME = 3;  // this is in seconds
        final int MAX_RETRIES = 5;
        
        //WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns 

successfully. 
        // The max wait time is 15 seconds(3 seconds with 5 retries)
        
        WebDriverWait wait = new WebDriverWait(driver, WAIT_TIME);
        int retry = 0;
        while (true)
        {
            try
            {
             wait.until(ExpectedConditions.visibilityOf(vElement)).click();
                return bClickOnElement;
            }
            catch (Exception e)
            {
                if (retry < MAX_RETRIES)
                {                 
                    retry++;
                    continue;
                }
                else
                {                 
                 bClickOnElement = false;
                 throw new ElementNotVisibleException("Click on Element failed");
                 
                }
                
            }
        }
    }


If you want to do selenium wrapper automation for drop down then please click in below link

Drop down using Selenium Wrapper

Driver Wrapper Automation

Database Operations



How to handle drop down using selenium wrapper automation

Drop down is very common element in automation and there is many to handle drop down.

Selenium wrapper automation is very good approach to handle drop down in automation.

We can perform below actions on drop down.

  • Select option using text
  • Select option using index
  • Select option using value
  • Get all options

I have created one class 'DropDownFunction' and which contains all the above methods. You can use all methods with object of that class.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public class DropDownFunction 
{ 
 public void mSelectByText(WebDriver driver, WebElement wDropdown, String vText)
 {  
  try
  {
   Select Dropdown = new Select(wDropdown);
   Dropdown.selectByVisibleText(vText);
  }
  catch (Exception E) 
  {
   System.out.println("Error in selecting dropdown option by visible text.");
  }  
 }
 
 public void mSelectByIndex(WebDriver driver, WebElement wDropdown, int vIndex)
 {  
  try
  {
   Select Dropdown = new Select(wDropdown);
   Dropdown.selectByIndex(vIndex);
  }
  catch (Exception E) 
  {
   System.out.println("Error in selecting dropdown option by index.");
  }  
 }
 
 public void mSelectByValue(WebDriver driver, WebElement wDropdown, String vValue)
 {  
  try
  {
   Select Dropdown = new Select(wDropdown);
   Dropdown.selectByValue(vValue);
  }
  catch (Exception E) 
  {
   System.out.println("Error in selecting dropdown option by value.");
  }  
 }
 
 public List<String> mGetAllOptions(WebDriver driver, WebElement wDropdown)
 {   
  List<String> Option = new ArrayList<String>(); 
  
  try
  {
   Select Dropdown = new Select(wDropdown);
   
   List<WebElement> temp = Dropdown.getOptions();
   
   for(int i=0;i<temp.size(); i++)
   {
    Option.add(temp.get(i).getText());
   }
   
  }
  catch(Exception E)
  {
   System.out.println("Error in getting options from dropdown");   
  }  
  return Option;
 } 
}

Popular