Extension Method in Selenium, what, why, and how???

An Extension Method enable us to add methods to existing   types without creating a new derived type, recompile, or modify the original types.

Alright, confusing???

Picture1

Now consider you don’t have access for Car class and if I ask you to add a method for Car class, how would you do that?

In this kind of circumstances you can use Extension method concept to add a method for existing type without creating new class.

Still???

If I ask you write a program which should give today date, probably you would write below lines of code-

Picture2

Now if I ask you to give 1 month back date, probably you would write below method to get 1 month back date-

Picture3

Certainly you would write this method in a class and to call this method you would create object.

Picture4

How it would be if you get same method like this-

Picture5

You can achieve above line of code using Extension Method concept.

Picture6

Now you will see your extension method in below manner-

Picture7

OK, there are some rules also-

  • It should be static method.
  • It must be located in a static class.
  • It should use the “this” keyword as the first parameter with a type in .NET and this method will be called by a given type instance on the client side.
  • An extension method should be in the same namespace as it is used or you need to import the namespace of the class by a using statement.
  • You can give any name for the class that has an extension method but the class should be static.
  • If you want to add new methods to a type and you don’t have the source code for it, then the solution is to use and implement extension methods of that type.

Picture8

Now you will see your selenium extension method in below manner-

Picture9

Alright, now we will see a real time example-

While automating an application sometimes we all observed that click action performs the operation but button does not get clicked.

In that scenario, mostly we write different line of codes or methods to achieve our goal.

So instead of writing code each time for specific click, we can write different extension methods to handle all possible click actions and if one click is not working out we can call another IWebElement click methods (Extension Method).

It’s a right approach in any Automation framework to keep extension methods to perform the similar kind of actions using different ways.

 

How to identify Windows ToolBar using CodedUI

Below code will help to click on buttons on tool bar which generally comes when we try to login into the application or try to download any file from the application.

As first action we do on the webpage so first we have to identify the webpage and then tool bar which pop-ups after performing the some actions. We have to use WinToolBar class to identify Tool Bar. So here you go-

BrowserWindow MainWin = new BrowserWindow();
MainWin.SearchProperties[BrowserWindow.PropertyNames.Name] = “Page Title Name”;
WinToolBar ToolStrip = new WinToolBar(MainWin);
ToolStrip.SearchProperties[WinToolBar.PropertyNames.Name] = “Notification”;
ToolStrip.DrawHighlight();
WinButton No= new WinButton(ToolStrip);
No.SearchProperties[WinButton.PropertyNames.Name] = “No”;
No.DrawHighlight();
Mouse.Click(No);

ToolBar1

 

Difference between @Factory and @DataProvider annotations

Both the annotations belong to TestNg testing framework. Many people get confused while reading about these two annotations that why and where we have to use these two?

Very basic difference between these two are-

@DataProvider- DataProvider annotations parameterize the particular test method and execute the test number of times based on data provided by DataProvider method. So if two parameters are there then test will execute twice.

@Factory- Factory executes all the test methods present inside any test class file using separate instance of the respective class.

Still confused???

Let’s have look on some programs-

@DataProvider
public Object[][] message(){
return new Object [][]{{“Mayank” , new Integer (321)}, {“Dileep”, new Integer (282)}};
}

@Test (dataProvider=”message”)
public void PrintMsg(String name, Integer id){
System.out.println(“Names are: “+name+” “+id);
}

As above program states that there are two data and if I run this program result will be-

Result1

So, it proves that if you increase the number of data in message method, PrintMsg method will execute same number of times.

Now we will see Factory and how it is different from DataProvider-

public class TestFactory {
@Factory
public Object[] factorymethod(){
return new Object[]{new DPandFactoryExaple(), new DPandFactoryExaple()};
}}

public class DPandFactoryExaple {
@DataProvider
public Object[][] message(){
return new Object [][]{{“Mayank” , new Integer (321)}, {“Dileep”, new Integer (282)}};
}
@Test (dataProvider=”message”)
public void PrintMsg(String name, Integer id){
System.out.println(“Names are: “+name+” “+id);
}
@Test
public void PrintSuccessfullMessage(){
System.out.println(“Print the successfull message”);
}}

So if we see DPandFactoryExaple class then you will find there are two @Test methods and 1 @Test method has to execute twice and 1 @Test method has to execute once. So there should be 3 messages.

Now if you see @Factory method it is calling the same class twice and in that case there should be 6 messages. See the results below-

Result2

So did it clear the doubts?? No, then comment it out we will discuss it.

 

How to include screenshots in ReportNG report?

Any test automation report is incomplete without the screenshots and including them in ReportNG is not that much difficult. In today’s post we will see how we can include the screenshots in ReportNG report. If you are wondering how to configure ReportNg with selenium then you can refer my previous post.

Here I am again considering that you already have below set-up-

  • Your project is already set-up in Eclipse
  • Project already has testng.xml file.
  • ReportNG is already set-up with Selenium and TestNG.

If above conditions are true then you just need to complete two steps-

1: Make it Escape_Property=false like this-

Screenshot

2: Now take the screenshot after some actions and pass through Reporter.log method to ReportNG report-

Screenshot1

Now run your testng.xml file and open the test report. If you have configured ReportNG properly and above set-up then you report should look like this-

screenshot3

How to configure ReportNG with Selenium WebDriver?

Selenium WebDriver is open source test automation framework which does not come with reporting facility. So selenium user has to be depended upon the third party plug-ins.

ReportNG is a simple HTML reporting plug-in for the TestNG unit-testing framework. It is intended as a replacement for the default TestNG HTML report. There are very few steps to configure ReportNG with WebDriver+TestNG.

To configure it we need three jar files (Guice, ReportNG, and Velocity-dep) which you can download from here-

http://reportng.uncommons.org/

and

https://github.com/google/guice/wiki/Guice30

Once you have these three jar files, please include them in your project-

lib

Here I am considering that you already have TestNg.xml file inside your project; if Yes, please include below listeners inside suite tag-

<listeners>
<listener class-name=”org.uncommons.reportng.HTMLReporter”/>
<listener class-name=”org.uncommons.reportng.JUnitXMLReporter”/>
</listeners>

Once you done, your testng.xml file should look like this-

TestNg

Now last step, disable the default testng listener by-

Right click on Project folder > click on Properties > click on TestNG which visible in left pane.

Test

All set, now run your testng.xml file and refresh your project. Once all done, It will add ‘test-output’ folder. Go to test-output > html > and open index.html to see the report.

report

Your report should look like this after all above set-up-

reports

How to configure Properties file with Selenium WebDriver?

Why Properties file?

To hide sensitive information of the projects we save the information in properties file.

To configure Properties file, create a new file and save as with .properties extension. Now add the data in following manner in Properties file:

URL=http://www.google.com
INT_USR=msrivastava
INT_PWD=Hello123&

Now use below lines of code to read this property file-

try{
FileInputStream prop = new FileInputStream (“D:\\Selenium_Projects\\Property\\setup.properties”);
Properties a = new Properties ();
try {
a.load(prop);
} catch (IOException e) {

e.printStackTrace();
}
String URL=a.getProperty(“URL”);
String USR=a.getProperty(“INT_USR”);
String PWD=a.getProperty(“INT_PWD”);
System.out.println(URL);
System.out.println(USR);
System.out.println(PWD);
}catch (FileNotFoundException e){
e.printStackTrace();
}

How to configure Selenium Grid?

Here I noted down some quick steps to configure Selenium Grid on windows. I am assuming that you are aware about selenium and its features. So here we go-

  • Download Selenium Standalone Jar file from here.
  • Dump it somewhere in host and node machines.
  • Open CMD at Host machine and go to location where you have been dumped the jar file.
  • Now run this command Java –jar seleniumserver-standalone_version.jar –role hub.
  • Now open http://localhost:4444/grid/console.
  • Now go to Node machine.
  • Open CMD at Node machine and go to location where you have been dumped the jar file.
  • Run the below commend if machine is remote-

java –jar selenium-server-standalone_version – role webdriver –hubHost HOST MACHINE NAME – “browserName=iexplorer, platform=WINDOWS” –port 5556

  • Run the below commend if machine is same machine where host is running-

java –jar selenium-server-standalone_version – role webdriver – “browserName=iexplorer, platform=WINDOWS” hubHost http://localhost:4444/grid/register –port 5555