2013-01-02

How to install TestNG in Eclipse?

In this article we are going to see how to install TestNG in Eclipse IDE to use with selenium webdriver. I am using j2ee with Eclipse (Indigo Service Release 2).

For the basic webdriver and Eclipse IDE setup, please follow my old post .

Please go through following steps to setup TestNG in eclipse

Step 1 : Run Eclipse and Click "Help"


Step 2 : Click "Install New Software"

Step 3 : Click "Add" from right upper side
 

Step 4 : Write "TestNG" as Name and "http://beust.com/eclipse" as Location

Step 5 : Click Ok and you see TestNG loaded. Select that check box  and press next
 

Step 6 : Complete the installation process by accepting , allowing terms & conditions . In the last point Eclipse will download and install total package. It will then prompt for restarting Eclipse. Restart Eclipse.

We have completed installation. Now let's check.
- Click Window -> Show View -> Others

We will get a view selecting window.

Select TestNG under Java and Click ok. We can see there is another view added in the bottom(my preference) of the eclipse.


To Run a project as TestNG project, we can get another option from Run -> Run As -> "TestNG Test".
 

Now We are ready to run our selenium test with TestNG. 

Why we need TestNG instead of JUnit. The main advantages for using TestNG are
- All major JAVA IDEs  EclipseIntelliJ IDEA, and NetBeans support TestNG.
- TestNG can be integrated in Apache Maven build system with custom tasks for Apache Ant (Selenium test can be run on nighty builds)
Continuous Integration using Hudson has built in support for TestNG.
There are more at TestNG website.

In separate post, I will write how to write code for TestNG in selenium web driver. 
...Thanks...:) 

2012-12-31

How to take a screenshot in selenium webdriver?

In Following article we are going to see how to take screenshot in selenium webdriver. I am using c# with VS2010.

Screenshot is a very important factor while testing in selenium. When ever any important functions need to be tested, we should take screenshot after testing . We may use this to take  screenshot after fail a test case or, to validate a test from previous reference or, to keep log of test results. lets start.
[Please follow my introductory selenium setup post to know about the basic selenium setup.]


Step 1 : After initializing driver , make a screenshot type variable (myScreen).

private Screenshot myScreen;

Step 2 : In the test method, After a test, assign myScreen by Casting the driver with ITakesScreenshot interface and call GetScreenshot()
myScreen = ((ITakesScreenshot)driver).GetScreenshot();

Step 3: Save the screenshot that we have taken.
myScreen.SaveAsFile(@"F:\ShantonuTest.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

So, For better understanding , I am pasting my full test code here

[Test]
public void TestHome()
{
driver.Navigate().GoToUrl(path);
Assert.AreEqual("Software Development Outsourcing. Offshore Software Development Company - Kaz Software", driver.Title);
myScreen = ((ITakesScreenshot)driver).GetScreenshot();
myScreen.SaveAsFile(@"F:\ShantonuTest.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
Note : We can skip step1, 2 and 3 by doing all together inside the test method.
 ((ITakesScreenshot) driver).GetScreenshot().SaveAsFile(@"F:\ShantonuTest.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

After the test method execution, my screenshot was found in f:\


In here we have used the

1. GetScreenshot() - takes no parameter but returns a OpenQA.Selenium.Screenshot object containing the image.

2. SaveAsFile("FullPathWithFileName", [System.Drawing.Imaging.ImageFormat object indicating file format]) -Takes two parameter but gives no output.

A Screenshot type object(myScreen from step 1) have extra following property
-myScreen.AsByteArray
We can use it to get the value of the screenshot image as an array of bytes.

-myScreen.AsBase64EncodedString
We can use it to get current System.Object as String (System.String type)

....Thanks...:)...

What is Load Testing? How to do it

2012-12-27

How to run selenium webdriver in Google Chrome?

In this article we are going to see necessary steps for running selenium web driver code in Google Chrome. I am using c# code with VS2010.

Step 1 : Install Google Chrome in your PC from this link

Step 2 : Install Google Chrome Driver from this link

Step 3 : After adding dll in properties of the project , we have to add the references in the class.
using OpenQA.Selenium.Chrome;[For basic idea on how to set up selenium environment, see the post]

Step 4:
Then we have to initiate the driver object as Google Chrome Driver.
private IWebDriver driver  = new ChromeDriver();

Now this driver can be used Just Like as other Firefox or IE drivers.

We can initiate Chrome Driver in 6 ways(overridden).

1.With no parameter
-ChromeDriver();
2.With a path [need when running remotely]
-ChromeDriver("stringPath")
3. With an Option
-ChromeDriver(options)
4. With a path and an Option
-ChromeDriver("stringPath", options)
5. With a path, an option and Timeout(The maximum amount of time to wait for each command.)
-ChromeDriver("stringPath",options, cmdTout)
6. With an option, timeout and a specific driver Service.
- ChromeDriver(service, options,cmdTout)

Note : In every case ,
-"stringPath" is the path to the directory containing ChromeDriver.exe. Ex "C:\Users\ssarker\Downloads"
- option  is an object of OpenQA.Selenium.Chrome.ChromeOptions
- cmdTout is an object of System.TimeSpan
- service is an object of OpenQA.Selenium.DriverService

Chrome driver will be found in this link. Install windows edition. You can get more detail here.
Note :
It is better to use with string. I use following code for initiating chrome driver(with chrome driver location).
public IWebDriver driver = new ChromeDriver(@"C:\") ;
This string can be other remotely located PC accessible over LAN.
There might be error for version on chrome console(i found, expected version is less then found version). To avoid the error, download latest web driver and chrome.

I will continuously update note section incrementally(after I find and solve problems regarding running in Chrome)

...Thanks...:)...

2012-12-26

How to change user agent in Firefox by selenium webdriver

In this article , we will see the function to change user agent of Firefox using selenium webdriver. I am using c# with vs 2010.
From my administrating Firefox post, we know that we can change preferences. I have another post describing different user agent(how to change user agent in jmeter) . So, lets change the agent from webdriver so that when ever we need we can use Firefox to change its agent to test different behavior of a web application.

First declare a Firefox profile. Then change general.useragent.override with a value(desire user agent string, any from here or here )

public static FirefoxProfile myFireProfile = new FirefoxProfile();

myFireProfile.SetPreference("general.useragent.override", "our_string");

public IWebDriver driver= new FirefoxDriver(myFireProfile);

So, our Firefox driver initializes with new user agent .

Thanks...:) 

How to work with Proxy in Selenium WebDriver

In this article we are going to see the functions for the configuration of proxy while testing a web application(under different proxy) . The are various property to control proxy settings of a browser. In here I am using c# with VS 2010.

Selenium webdriver has build-in class to maintain proxy settings.  We have to initialize a proxy object.We can initialize in 2 ways.

private Proxy myProxy = new Proxy();

Another one with proxy settings string as dictionary(multiple settings initialization) .
new Proxy(Dictionary settings)

A proxy object has seven properties . These properties are used for several purposes.

-To Get or set the value(string) of the proxy for the FTP protocol.
myProxy.FtpProxy;

-To Get or set the value(string) of the proxy for the HTTP protocol.
myProxy.HttpProxy;

-To Get or set a value(Boolean) to know proxy uses autodetection.
myProxy.IsAutoDetect;

-To Gets or sets the value(string) for when no proxy is specified.It will overwrite the proxy settings.
myProxy.NoProxy;

-To Get or set the URL(as string) used for proxy Auto Configuration. 
 myProxy.ProxyAutoConfigUrl

 -To Get or set the value(string) of the proxy for the SSL protocol.
 myProxy.SslProxy

-To Get or set the type of proxy(enum).
mProxyKind = myProxy.Kind;

Note : There is an Enum(ProxyKind) for identifying the proxy type.
The ProxyKind enum members are :

Member name
Value Description
Direct 0
Direct connection, no proxy (default on Windows).
Manual 1
Manual proxy settings (e.g., for httpProxy).
ProxyAutoConfigure 2
Proxy autoconfiguration from URL.
AutoDetect 4
Use proxy autodetection.
System 5
Use the system values for proxy settings (default on Linux).
Unspecified 6
No proxy type is specified.


In here, These are property functionality of a proxy object. I will provide example with proxy configuration in separate post where we can see different configurations for different proxy(like NTLM, CC proxy, Win Gate etc).

...Thanks...:)