Sunday, 21 October 2012

Avoid explicit delay in selenium


If you are using java with selenium for automation,then you should use explicit delay in your code(like Thread.sleep(2000)).

Here is an example for same:

WebDriverWait wait = new WebDriverWait(webDriver, 5);//here 5 is a maximum waiting time in seconds
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@value='SignIn']")));

//ExpectedConditions have lot of functions for different actions,so use these functions according your requirement.

Mouse over on "element" using selenium



Here is is example of mouse over

Actions builder=new Actions(driver);
builder.moveToElement(tagElement).build().perform()

Saturday, 20 October 2012

How to move cursor in selenium.



In automation testing,you need cursor movement for displaying hidden elements.

Here is a example:

public void mouseOverOnElement(WebElement tagElement)
{
try
{
Actions builder=new Actions(driver);
Point coordinate=tagElement.getLocation();
Robot robot=new Robot();
builder.moveToElement(tagElement, 5,5).build().perform();
builder.moveByOffset(1, 1).build().perform();
robot.mouseMove(coordinate.getX()+8,coordinate.getY()+60);

}
catch(Exception e)
{
logger.info("Error message in mouseOverOnElement method-->"+e);
}
}

Using the above code you are able to move mouse cursor.

Wednesday, 17 October 2012

How to rerun failed testcases using testng.



If you want automatically rerun your failed testcases after completion,then use below approach:
Note:i think you know that whenever testcase is failed in testng,it creates a "testng-failed.xml" for failed testcases.

1)if you want to rerun automatically only failed testcases then you need modify pom.xml.
2) Run time you need to change testng.xml file path to "testng-failed.xml" in pom.xml.This you can do using any script language.I have done same using batch script.
Below is the batch script for same:

setlocal enabledelayedexpansion
set INTEXTFILE=pom.xml
set OUTTEXTFILE=pom_out.txt
set SEARCHTEXT=src/main/resources/testng.xml
set REPLACETEXT=target/failsafe-reports/testng-failed.xml
set OUTPUTLINE=

call mvn integration-test


for /f "tokens=1,* delims=¶" %%A in ( '"type %INTEXTFILE%"') do (
SET string=%%A
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!

echo !modified! >> %OUTTEXTFILE%
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%

call mvn integration-test

for /f "tokens=1,* delims=¶" %%A in ( '"type %INTEXTFILE%"') do (
SET string=%%A
SET modified=!string:%REPLACETEXT%=%SEARCHTEXT%!

echo !modified! >> %OUTTEXTFILE%
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%

3) Now start execution using above batch file.

Tuesday, 16 October 2012

How to handle CAPTCHA and certificates in selenium automation



If you are using firefox browser with selenium and you are facing captcha and certificates(for https traffic).
Then use same profile for handling captcha and certificate.

Refer the following link for "How to create a firefox profile".
http://seleniumproblemswithsolutions.blogspot.in/2012/10/how-to-create-profile-in-firefox-for.html

Now you can use same profile in webdriver using below code.
File f=new File("profile folder name");
FirefoxProfile firefoxProfile=new FirefoxProfile(f);
WebDriver driver = new FirefoxDriver(firefoxProfile);

How to create a profile in firefox for selenium.


If you are worried about how to handle CAPTCHA and firefox certificates.
Then use firefox profile for solving these problem.
Below are the steps for same.

1. Make sure all your firefox instance are closed
2. Click Start>Run
3. Type “firefox.exe -ProfileManager -no-remote”
4. Select “Create Profile” (i.e. selenium)
5. Click “Next”
6. Enter new profile name
7. Select a directory folder to store your new profile
8. Click “Finish”
9. Select “Don’t ask at startup”
10. Click “Start Firefox” and configure settings based on suggestion below***
11. Set Profile back to “default” (enable you to use your previous settings on your browser)
*** Suggested settings for your Selenium Profile
1. From “View\Toolbars” tab, uncheck “Bookmarks Toolbar”
2. Right click from toolbar and click “Customize”
3. Remove “Google search” by dragging it to the “Customize Toolbar” window
4. From the “Customize Toolbar” window, click “Use Small Icons” check box then hit “Done”
5. Click “Tools\Options” then set the following:
a. “Main” tab
- set Home Page to “about:blank”
- uncheck “Show the Downloads..” option
b. “Tabs” tab
- Select “a new window” for new pages
- Uncheck all warning options
c. “Content” tab
- uncheck “Block pop-up” windows option
d. “Privacy” tab
- uncheck all “History” options
e. “Security” tab
- uncheck all “Security” options
- click “Settings” and uncheck all warning options
f. “Advanced” tab
- Uncheck “autoscrolling” option from “General” tab
- uncheck “warn me …” and “Ssearch Engines”option from “Update” tab
7. From the address bar type “about:config” and add the following by right-click anywhere on the page and selecting “new”
- extensions.update.notifyUser (type=boolean; value=false)
- extensions.newAddons (type=boolean; value=false)

For more details refer following url:
http://kb.mozillazine.org/Creating_a_new_Firefox_profile_on_Windows

Saturday, 13 October 2012

How to immediate rerun failed testcase using testng.


After developing a automation framework,now a automation tester want to reduce a false positive rate,so he/she will use immediate rerun of failed test case for reducing the failed test due to setup and network problem.

Below is the code for same:

1) Create a class with your rerun logic

Using the below logic you can immediate rerun your failed test case.
package com.companyName.web.utils;

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class Retry implements IRetryAnalyzer{
private int retryCount = 0;
private int maxRetryCount = 3;
public boolean retry(ITestResult result) {

if(retryCount < maxRetryCount) { retryCount++; return true; } return false; } }

2) use below annotation in your test program
@Test(groups="groupName",retryAnalyzer=Retry.class)
if you are not using group then use this
@Test(retryAnalyzer=Retry.class)