Tuesday, October 25, 2011
I had earlier covered in my blog on how to execute your RC cases in selenium grid. But with the commencement of Selenium-2/Webdriver, grid setup has been changed. In the following blog I will cover how to set-up your grid and the changes that will be required for easy execution of cases.
Following things are required:
1. Selenium 2.xx version server jar and Java library. The latest one can be downloaded from the link: Selenium-2
2. Java 1.6 and above
3. TestNg jar . You can download it from the link: TestNG
4. Eclipse with TestNG plugin installed(optional)
5. ChromeDriver. Can be downloaded from: ChromeDriver
The Test Code
Following is an example of test-class that have a test case to search google for "testing" and verifying after clicking it on a link.
In the above class I am using the TestNG "Parameter" property to provide different data set to the "BeforeClass" method "beforeClass". The beforeClass method accepts two properties "browser" and a "port".
For Hub:
For a firefox based node:
For google-chrome based node:
You can do configurations for environment like OS (Linux,Windows), browser, browser-version and all and then you can run you cases on particular type of environment by configuring them accordingly. More details can be found at the following URL:
http://code.google.com/p/selenium/wiki/Grid2
Following things are required:
1. Selenium 2.xx version server jar and Java library. The latest one can be downloaded from the link: Selenium-2
2. Java 1.6 and above
3. TestNg jar . You can download it from the link: TestNG
4. Eclipse with TestNG plugin installed(optional)
5. ChromeDriver. Can be downloaded from: ChromeDriver
The Test Code
Following is an example of test-class that have a test case to search google for "testing" and verifying after clicking it on a link.
In the above class I am using the TestNG "Parameter" property to provide different data set to the "BeforeClass" method "beforeClass". The beforeClass method accepts two properties "browser" and a "port".
These values are used for initialization of driver and in-turn for initialization of selenium object. In the above code I am using the "WebDriverBackedSelenium" class for creation of the selenium object, so that its easy for guys who had worked on Selenium-1 to understand the code. If you want the code to be purely WebDriver, you can directly use the "driver" object for defining your test-cases.
The main part in this test case is how the driver object is being created:DesiredCapabilities capability= new DesiredCapabilities();
capability.setBrowserName(browser);
WebDriver driver= new RemoteWebDriver(new URL("http://localhost:".concat(port).concat("/wd/hub")), capability);
The above code creates an object of DesiredCapability and then set the browser value to it. Now using this "capability" object I am creating the webdriver object using the "RemoteWebDriver" class. This tells the selenium on which browser the test-case needs to run and where the server is located. In this example I am assuming the server to be running locally. In case it is on different system the "localhost" needs to be re-placed with the ip of the said system.
TestNG configuration
For parallel execution you need to use the TestNG configuration. Following is an "testng.xml" file for the above said test class. The said configuration executes the test-cases across different browser.
<suite name="Selenium TestNG Suite" parallel="tests"
thread-count="5">
<test name="Selenium TestNG - 1">
<parameter name="browser" value="firefox" />
<parameter name="port" value="4444" />
<classes>
<class name="com.test.testng.Google" />
</classes>
</test>
<test name="Selenium TestNG - 2">
<parameter name="browser" value="chrome" />
<parameter name="port" value="4444" />
<classes>
<class name="com.test.testng.Google" />
</classes>
</test>
</suite>
In the above configuration, I am configuring TestNG to run "tests" in parallel. Also there are two different tests inside a suite. For each test a different "browser" parameter value has been configured.
Selenium-Grid server
Now start your grid using the following commands. Run each command in a seperate terminal or command prompt by going to the directory containing your selenium-server-standalone-2.x.x.jar. In the following example I am using the 2.7.0 version of selenium.
For Hub:
java -jar selenium-server-standalone-2.7.0.jar -role hub
For a firefox based node:
java -jar selenium-server-standalone-2.7.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=firefox
For google-chrome based node:
java -Dwebdriver.chrome.driver=/path/to/chromedriver -jar selenium-server-standalone-2.7.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5555 -browser browserName=chrome
Before running the above command you need to provide the chrome-driver path to the property "-Dwebdriver.chrome.driver".
Now run your testng.xml from eclipse by selecting it -> Right click -> Run as -> TestNG suite(this will work only if you have TestNG plugin installed in your eclipse.)
Or you can choose other ways to execute "testng.xml" like from command prompt, using ant or maven.
Once you run the above testng.xml. TestNG will execute the cases from the Google class on grid across different browsers firefox and google-chrome as configured.
You can do configurations for environment like OS (Linux,Windows), browser, browser-version and all and then you can run you cases on particular type of environment by configuring them accordingly. More details can be found at the following URL:
http://code.google.com/p/selenium/wiki/Grid2
Thursday, October 20, 2011
Most of the guys who are into
automation may be knowing the term Data-Driven testing. But the word
will still be new for some fresh faces in the field of automation. In this blog I will explain what is Data-Driven testing and will give an example of Data-driven testing using Junit and TestNG frameworks.
Data-Driven testing as the name
suggests is a test driven by the Data. For ex. You have a user detail
form where you need to enter details of multiple users and save them.
Here if you have 5 different user data available and you have to
write automation cases for these, you may end up writing 5 different
automation test cases(one for each user data).
If you apply a Data-Driven approach you
will end up with only one test-case, that is filling the form with
user data and then submitting it. The test case will get executed
based on the data provided to it. In this case it will be 5 and hence
the test case will get executed 5 times with different data-set.
The advantage of using a
Data-driven approach is that you reduce your effort in writing/maintaing test-cases for your different type of data. In case of
additions or deletion of new/old entries , you just have to change the
data and not your actual test-case.
Following I will mention a Data-Driven
approach for searching on google with different data using Junit and TestNg frameworks:
Using Junit:
Using TestNg:
Using Junit:
Using TestNg:
The main difference in the above two functions is that you provide a Paramaterized option to the class in Junit and supply data to the constructor of the said class. Where as in TestNG you do the same at the test-method level.
Its simple to do data-driven testing in TestNG framework as you can provide a different data providing function for each test-method, but the same is not possible in Junit.
Tuesday, October 4, 2011
While automating your web-application using webdriver, many may have faced problems in executing your cases on Google-chrome. Whenever you try to use webdriver object for executing your cases on chrome using the following code:
you most probably get the following error:
Method - 1:
Start the selenium server using the following command:
DesiredCapabilities capability = DesiredCapabilities.chrome();
you most probably get the following error:
The path to the chromedriver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromium/downloads/list
The solution to this error that has been mentioned in the given URL "http://code.google.com/p/selenium/wiki/ChromeDriver" is to set the chromedriver path to the variable "webdriver.chrome.driver". But how exactly to set the path and use it for your test execution is not given there. So I though to just document it here on my blog.
For setting the "webdriver.chrome.driver" value and for using google-chrome to execute your automated tests, you can use one of the following methods:
Method - 1:
Start the selenium server using the following command:
java -Dwebdriver.chrome.driver=/path/to/chromedriver -jar selenium-server-standalone-2.7.0.jar
And in your Java code you use the chrome driver in the following way:
DesiredCapabilities capability = DesiredCapabilities.chrome();
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
The advantage of using this method is that you can execute your test-cases in a remote machine and this is advantageous when you want to run it on grid.
Method - 2:
Set the system property "webdriver.chrome.driver" using System.setProperty() in your code and then try to execute your cases on Chrome.
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
This method works well when your tests are getting execute on a local machine and you are not starting any selenium server.
Following are some important links for Chrome driver:
Webdriver Chromedriver page:
http://code.google.com/p/selenium/wiki/ChromeDriver
ChromeDriver download page for downloading the ChromeDriver server:
http://code.google.com/p/chromium/downloads/list
Thursday, September 29, 2011
While doing an automation I came up with a problem where I need to get a list of all files in a Directory.
So just though to share the same with you the program to do so. I had made used of recursion in the following programs to get the list of files. Following are the two programs one will give you a ArrayList of files with their absolute path where as another will give you an ArrayList of File object of respective files.
Following program will store the absolute path of all the files inside a directory to the fileList argument provided.
Following program will store the File object of the files that are found into the respective fileList Array provided as an argument to the function
So just though to share the same with you the program to do so. I had made used of recursion in the following programs to get the list of files. Following are the two programs one will give you a ArrayList of files with their absolute path where as another will give you an ArrayList of File object of respective files.
Following program will store the absolute path of all the files inside a directory to the fileList argument provided.
Following program will store the File object of the files that are found into the respective fileList Array provided as an argument to the function
Friday, September 23, 2011
As there are more and more development happening on the android front, automation of the developed applications has also become a necessity. Android within its API supports a testing framework which can be used for testing applications, but writing and developing cases using that API is not easy. We may need a framework that makes it easy to write and develop test cases for our applications.
One of such framework is "NativeDriver". It is built over the Selenium webdriver automation framework.
As you must be knowing that selenium is vastly used open-source functional automation tool.
In this write-up I will tell you on how to automate an android application using NativeDriver
Following things are required before starting:
- Android SDK 2.2 or later - download
- Eclipse version 3.5 or later (Eclipse IDE for Java Developers recommended) - download
- Android Development Toolkit (ADT) plug-in - Installing ADT
- Ant - download
- JDK 1.6 or later - download
Building the NativeDriver libraries:
- Checkout the NativeDriver code:
svn checkout https://nativedriver.googlecode.com/svn/trunk nativedriver --username {Google account e-mail address}
- Build the NativeDriver code:
$ cd nativedriver/android $ ant
The libraries are built and stored in the nativedriver/android/build directory:
- server-standalone.jar - this should be linked to your Android application, and runs on the Android device (or emulator). This is the NativeDriver server, and listens for requests to automate the application, such as “start an activity” or “click a UI element”
- client-standalone.jar - this should be linked to the test, which is the NativeDriver client. It implements a WebDriver API which communicates with the server.
Adding the NativeDriver jar to your application
- Import your android application code into the eclipse by using the File -> Import functionality of eclipse.
- Add the server-standalone.jar to the build-path of your android application by Right clicking on the Android application project -> Configure build path -> Add jars or Add external jars (browse to the server-standalone.jar file, select and add it).
- To the application AndroidMainfest.xml file, add the following to the
element.
<instrumentation android:targetPackage="{app_package_name}"
android:name="com.google.android.testing.nativedriver.server.ServerInstrumentation" />
android:name="android.permission.INTERNET" />
android:name="android.permission.WAKE_LOCK" />
android:name="android.permission.DISABLE_KEYGUARD" />
Here {app_package_name} needs to be replaced with the name of the package as specified in the mainfest element's package attribute.- Build the application and install it on the device.
- Now go to the commandline and start the instrumentation using the following line:
adb shell am instrument {app_package_name}/com.google.android.testing.nativedriver.server.ServerInstrumentationHere again the {app_package_name} needs to be replaced with the name of the package as specified in the mainfest element's package attribute.
- Enable port forwarding by using the following command:
adb forward tcp:54129 tcp:54129
Writing your Test Cases
For writing your test cases you just have to create a new Java project and start writing code for it. I will explain this using a code example. The following code is done to automate an opensource K9 mail application on android:
Important Notes:-
- As there is no recording feature available, we need to write the code manually for automating our scenarios.
- For identifying the element id to interact on the UI, you can get the ID's of the elements from the R.java file generated inside your android application code.
- Also as we are writing our cases in Java code we can use our own test framework and generate html reports for out automation execution.
Note: Most of the material in this blog has been taken from the original site of the NativeDriver. The credit for such material goes to the original author.
Following is the link to it. http://code.google.com/p/nativedriver
Wednesday, August 17, 2011
Nowadays android is the latest thing in the market and many of the companies are working on android app development. With development comes the requirement to test an application too. As we all tester know that automation is one of the important part of testing, and is mainly used to reduce the time taken to execute the testcases. This gives time to testers to concentrate more on exploratory testing. Recently I was going through some group, and found out about some open-source frameworks that makes it possible to automate an Android native application. I went through two of such frameworks and will write about what I felt about them. The two frameworks I am going to talk about is Nativedriver and Robonium.
Nativerdriver- Is an opensource framework written over Selenium-2.0 Webdriver implementation. As we all know that selenium is a great functional automation tool for web-applications and gives you a cross-browser and cross-platform supports. The support for testing native application has enhanced the value of selenium. Nativedriver can be used when you are actually working along with the development team and has access to the source code of the andorid application that needs to be tested.
Robonium – Is also a great tool and is written by extending the Internal Android testing framework. This framwork supports a lot of functions which can be used to auotmate your application test. The main advantage of this framework is that , you can automate any application using this without actully having the source code of the app.
Advantages of Nativedriver:
- Built over the selenium webdriver hence can be easliy used by existing selenium users.
- Test are run as a normal Java unit test. And hence other frameworks can also be used for test-case execution or reporting.
- Ant or maven can also be used for execution of cases.
- Fast execution and reporting of testing results
- Need access to the source code of the application that needs to be tested.
- Sometime difficult to find element as support for finding elements by index is not available
Advantages of Robonium:
- Don't require the source code of the application for testing.
- Elements are easy to identify and work on.
- Tests are simple to write.
- Setup is simple.
- Can only be executed suing eclipse.
- Slow execution.
- Typing a normal text is not supported at this point.
Monday, August 8, 2011
Somebody recently asked me on how to scroll a page which is dynamically loaded. Something like the Facebook page where the updates gets loaded as you scroll down the page. As I never had such a requirement , I didn't knew a solution to it.
While searching I found people suggesting the "selenium.focus" function to be used as a solution, but that does not solve the problem I am talking about. The "selenium.focus" should be used when the element you are looking for gets loaded when the page gets loaded and not when you scroll through the page.
After lot of searching I was unable to get a proper answer to my problem and finally got an idea for implementing it. Its just a 4 to 5 lines of code that can be used in your code for scrolling on the page.
While searching I found people suggesting the "selenium.focus" function to be used as a solution, but that does not solve the problem I am talking about. The "selenium.focus" should be used when the element you are looking for gets loaded when the page gets loaded and not when you scroll through the page.
After lot of searching I was unable to get a proper answer to my problem and finally got an idea for implementing it. Its just a 4 to 5 lines of code that can be used in your code for scrolling on the page.
Following is the code:
The above code uses the JavaScript method "scrollBy" to scroll on the page. The for loop runs for 60 sec and calls the scrollBy function every second. This makes the selenium to scroll on the page.
If you have a test where you need to scroll on the page and check whether an element is loaded dynamically or not you can put a isElementPresent like function after the "driver.executeScript" to check visibility of your element.
Following is a test method written in webdriver that you can use to test the above function on the Facebook page:
The following code is written for Selenium-1.0 users:
Thursday, July 28, 2011
While going through Selenium-2.0 I found out that WebDriver does not have a function called isElementPresent(). This was one of the important functions that was used in Selenium-1.0. It was mainly used for waiting for an element to be available to take an action on it. To implement this in WebDriver you just need to write a method as mentioned below. You can then use this function with any type of "By"(By.id, BY.name, etc.)
The above function will return true in case the element is found on the page, else it will return false.
In case your want to wait for a element to be present you can implement it in the following way.
In case your want to wait for a element to be present you can implement it in the following way.
Monday, July 25, 2011
While doing automation testing many of us had thought that it is good to send the excetuion report mail after your test execution. In this blog I am going to tell two ways using which you can send a mail with to multiple E-mail addresses with your execution report attached.
Using ant:
Ant supports a mail task which can be used as one of your ways for sending a mail. Following target can be used to send a mail using ant:
In the above target you need to mention the following things:
mailhost – this the sender host of your mail box. You can this from your outgoing configuration of account in you mail client. For gmail it is “smtp.gmail.com”
port – this is the port at which the above mailhost support sending of email. This configuration also you can check in your mailbox configuration. By default it is “25” but for SSL and TLS support it will be different for different hosts.
User/password – some mail hosts need the username and password of the account mail box to authenticate the user while sending a mail. You need to provide this in the “user” & “password” attributes.
ssl – if you want to use ssl for contacting the mailbox you need to set this attribute value to “true”
subject – Subject of the mail you want to give.
to – you need provide the address of the email recepients here using the “address” attribute. You use a property also here. This property you can setusing the properties file and then importing to the build.xml
attachments – this can be used to attach your report as part of the mail and then send it.
Please note: If you get a MIME error or java mail error while running the above target. Please download a jar of “java mail” and put it to the lib folder of your ant installation.
Using Java code:
When I had this problem of sending the execution report as mail, I did it in a hard way by writing the following java code. In the following code I had used the javax mail API for composing and sending the mail.
Using ant:
Ant supports a mail task which can be used as one of your ways for sending a mail. Following target can be used to send a mail using ant:
In the above target you need to mention the following things:
mailhost – this the sender host of your mail box. You can this from your outgoing configuration of account in you mail client. For gmail it is “smtp.gmail.com”
port – this is the port at which the above mailhost support sending of email. This configuration also you can check in your mailbox configuration. By default it is “25” but for SSL and TLS support it will be different for different hosts.
User/password – some mail hosts need the username and password of the account mail box to authenticate the user while sending a mail. You need to provide this in the “user” & “password” attributes.
ssl – if you want to use ssl for contacting the mailbox you need to set this attribute value to “true”
subject – Subject of the mail you want to give.
to – you need provide the address of the email recepients here using the “address” attribute. You use a property also here. This property you can setusing the properties file and then importing to the build.xml
attachments – this can be used to attach your report as part of the mail and then send it.
Please note: If you get a MIME error or java mail error while running the above target. Please download a jar of “java mail” and put it to the lib folder of your ant installation.
When I had this problem of sending the execution report as mail, I did it in a hard way by writing the following java code. In the following code I had used the javax mail API for composing and sending the mail.
Thursday, July 21, 2011
People has kept asking me how to capture the width and height of an image/element using selenium. I tried to get an answer to it and the solution is very simple. The solution comes with Selenium itself.
Selenium-1 consists of certain functions for storing element values like getElementHeight, getElementWidth, getElementPositionLeft, getElementPositionRight, getElementPositionTop.
Similarly in Selenium-2(WebDriver) you can use the getSize() function which will return a "Dimension" class object. You can then use the getHeight() and getWidth() functions over it for getting the width and height of the element.
We can use these functions to get the position of the element and also its width and height.
Following is an example for getting the height and width of an image/element on the browser using Selenium-1.
Following is an example for getting the height and width of an image/element on the browser using Selenium-2(WebDriver).
Selenium-1 consists of certain functions for storing element values like getElementHeight, getElementWidth, getElementPositionLeft, getElementPositionRight, getElementPositionTop.
Similarly in Selenium-2(WebDriver) you can use the getSize() function which will return a "Dimension" class object. You can then use the getHeight() and getWidth() functions over it for getting the width and height of the element.
We can use these functions to get the position of the element and also its width and height.
Following is an example for getting the height and width of an image/element on the browser using Selenium-1.
height = selenium.getElementHeight("locaton to the image on the browser.(xpath or id)")
width = selenium.getElementWidth("location to the image on the browser.(xpath or id)")
Following is an example for getting the height and width of an image/element on the browser using Selenium-2(WebDriver).
height = driver.findElement("location to the image on the browser.(xpath or id)").getSize().getHeight();
width = driver.findElement("location to the image on the browser.(xpath or id)").getSize().getWidth()
Wednesday, July 20, 2011
While automating a web application some of us have to deal with the test to check that an image has been successfully uploaded or not.And that the uploaded image matches with the actual one or not. To automate this test, there are certain things that we need to achieve:
1. Saving the image.
Selenium does not support any direct way to actually store/get an image. But there are other ways like "right-click" and "Save as" option using which you can save the image on firefox and chrome. Once you get an image you can move further with the second step that is checking size of the image.
2. Handling the re-sizing and re-formatting of the image by the web application itself.
Whenever we upload an Image to a web-app it internally re-size and re-format the image while storing or rendering to save space and improve performance. So if you save an uploaded image from your web-app and want to compare it with the original image, it will fail. The reason being the image that you had downloaded from the web-app has been modified and may not match with the original image in terms of format, size and clarity. The solution to it can be achieved by getting the logic from the development team for re-sizing or re-formatting of image that they use inside their code. You can use then use the same code in you automation test to re-size and re-format your original image and then compare it with the downloaded image to check whether both matches or not.
Following code will give you a general idea on how to re-size an image.
3. Logic to compare images.
Once you had completed the above two steps you can go ahead with comparison of the images.
Following is a java code using which you can actually match two images pixel by pixel.
- Saving the image that has been uploaded for comparison with the original image.
- Handling the re-sizing and re-formatting of the image by the web application itself.
- Logic to compare images.
1. Saving the image.
Selenium does not support any direct way to actually store/get an image. But there are other ways like "right-click" and "Save as" option using which you can save the image on firefox and chrome. Once you get an image you can move further with the second step that is checking size of the image.
2. Handling the re-sizing and re-formatting of the image by the web application itself.
Whenever we upload an Image to a web-app it internally re-size and re-format the image while storing or rendering to save space and improve performance. So if you save an uploaded image from your web-app and want to compare it with the original image, it will fail. The reason being the image that you had downloaded from the web-app has been modified and may not match with the original image in terms of format, size and clarity. The solution to it can be achieved by getting the logic from the development team for re-sizing or re-formatting of image that they use inside their code. You can use then use the same code in you automation test to re-size and re-format your original image and then compare it with the downloaded image to check whether both matches or not.
Following code will give you a general idea on how to re-size an image.
public BufferedImage resize(BufferedImage img, int newW, int newH) { //Getting the width and height of the given image. int w = img.getWidth(); int h = img.getHeight(); //Creating a new image object with the new width and height and with the old image type BufferedImage dimg = new BufferedImage(newW, newH, img.getType()); Graphics2D g = dimg.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); //Creating a graphics image for the new Image. g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null); g.dispose(); return dimg; }
3. Logic to compare images.
Once you had completed the above two steps you can go ahead with comparison of the images.
Following is a java code using which you can actually match two images pixel by pixel.
import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.awt.image.Raster; import java.io.File; import java.io.IOException; public void compareImage() { boolean ret = true; try { original = ImageIO.read(new File( "originalFile")); copy = ImageIO.read(new File("copyFile")); ras1 = original.getData(); ras2 = copy.getData(); //Comparing the the two images for number of bands,width & height. if (ras1.getNumBands() != ras2.getNumBands() || ras1.getWidth() != ras2.getWidth() || ras1.getHeight() != ras2.getHeight()) { ret=false; }else{ // Once the band ,width & height matches, comparing the images. search: for (int i = 0; i < ras1.getNumBands(); ++i) { for (int x = 0; x < ras1.getWidth(); ++x) { for (int y = 0; y < ras1.getHeight(); ++y) { if (ras1.getSample(x, y, i) != ras2.getSample(x, y, i)) { // If one of the result is false setting the result as false and breaking the loop. ret = false; break search; } } } } } if (ret == true) { System.out.println("Image matches"); } else { System.out.println("Image does not matches"); } } catch (IOException e) { System.out.println(e); } }
Monday, July 18, 2011
Many people has asked me on how to run a test on different browsers using Junit without actually changing the value of browser after one test execution. For which I had always kept answering that it may not be possible with Junit. But recently while trying to find a solution for it I came across with the Parameterization option that is available with latest changes of Junit. This was not possible in earlier versions of Junit but now Junit has come up with lot of added options in the latest version of Junit 4.8. Following is a way by which you can execute your automation tests in multiple browsers without actually waiting to finish your tests and then passing the second browser value. See the example below:
import com.thoughtworks.selenium.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import java.util.Arrays; import java.util.List; @RunWith(Parameterized.class) public class TestGoogleBase extends SeleneseTestBase { private String browser; public TestGoogleBase(String browser){ this.browser=browser; } @Parameters public static List data() { return Arrays.asList(new Object[][]{{"*chrome"},{"*googlechrome"}}); } @Before public void setUp() throws Exception { selenium = new DefaultSelenium("localhost", 4444, browser, "http://www.google.co.in/"); selenium.start(); } @Test public void testUntitled() throws Exception { selenium.open("/"); selenium.type("id=lst-ib", "testing"); selenium.click("//input[@value='Google Search']"); for (int second = 0;; second++) { if (second >= 60) fail("timeout"); try { if (selenium.isElementPresent("link=Software testing - Wikipedia, the free encyclopedia")) break; } catch (Exception e) {} Thread.sleep(1000); } selenium.click("link=Software testing - Wikipedia, the free encyclopedia"); for (int second = 0;; second++) { if (second >= 60) fail("timeout"); try { if (selenium.isTextPresent("Software testing")) break; } catch (Exception e) {} Thread.sleep(1000); } } @After public void tearDown() throws Exception { selenium.stop(); } }
In the above class I am using the @RunWith(Parametrized.class) option that will tell Junit that this class is run as a parametrized class. A parameter value providing method is added to the class by denoting @Parameter annotation which returns a List< Object[]> array. This array consists of the browser names on which the test needs to be executed, like “*chrome”(Firefox) and “*googlechrome” in this case.
The constructor of the said class is modified to accept a “browser” string that is set when the said parameter function return the browser value.
This “browser” value is then used by the “setup” and inturn by the “Test” method while execution.
Friday, July 1, 2011
There are many functions provided by TestNG and you can use them in different ways I will mention in this blog about the @Factory implementation.
@ Factory
A Factory will allow to create test dynamically. And whenever we define a Factory it should always return an object array “Object[]”. All the test methods under the Class object which is created and returned using the Factory method is automatically executed by the Factory method. For ex.
public class FactoryClass { @Factory public Object[] createTest(){ Object[] res = new Object[3]; for(int i=1;i<=3;i++){ res[i-1]=new main.java.FactoryImplementation(i); } return res; } } public class FactoryImplementation { int c_instance; public FactoryImplementation(int instance){ this.c_instance=instance; } @Test public void printMethod(){ System.out.println("Instance Num is "+ c_instance); } }
Whenever you execute the above two classes together as a TestNg test, the Output will be as follows:
Instance Num is 1
Instance Num is 2
Instance Num is 3
You only have to execute the Class named “FactoryClass”. Test-NG will find the Factory method inside it and will execute it. For each loop inside the Factory method an object of FactoryImplementation Class is created. On creation of the FactoryImplementation object and Instance value is set using the constructor. While creation of FactoryImplementation object the Test-NG automatically searches for all the “Test” methods inside the FactoryImplementation class and add them to its test execution list.
Another thing for you to notice is that each object created have a unique “c_instance” value set while object creation and the same value is used in the “printMethod”.
The Factory method can be used in different ways. One of the case may be when you want to execute a certain number of test inside a class for different values that can increase or decrease based on a condition.
This can be achieved by creation of the object of test class based on the number of values.
In the above class I am creating the object of the FactoryImplementation class inside a "for" loop. In a similar way you can iterate the for loop for number of different values for which the test needs to be run. And then initializing the test class object with such values and returning the Object array after storing them in the array. Something like the following:
@Factory public Object[] createInstance(){ ArrayList ar = new ArrayList< Object>(); Object[] res =null; for(int i=1;i<=5;i++){ ar.add(new main.java.FactoryImplementation(i+10)); } res= new Object[ar.size()]; res=ar.toArray(); return res; }
Subscribe to:
Posts
(
Atom
)