Selenium creates a new and clean profile each time it runs a test, so it won't keep any cookies of the previous session and it wont use any browser plug-in however you can force selenium to use your own profile.
Steps
1. Close all instances of firefox browser.
2. Create a new firefox profile : Go to Run and type firefox -p and press OK . This will open a dialog bod where you create new firefox profile, have a name "nk" and save it in C: drive.
3. Install all plug-ins in this profile ; For SSL certificate issue install SSL security bypass
https://addons.mozilla.org/en-US/firefox/addon/10246/
4. Now you write you test cases like below
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
import java.io.File;
public class firefoxprofile extends SeleneseTestCase {
private SeleniumServer seleniumServer;
private Selenium selenium;
public static final String MAX_WAIT_TIME_IN_MS="60000";
@BeforeClass
public void setUp() throws Exception {
File template = new File("C:\\nk");
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setSingleWindow(true);
rc.setFirefoxProfileTemplate(template);
seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
seleniumServer.start();
selenium.start();
}
@Test
public void googling() {
selenium.open("/");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
selenium.type("q", "http://automationtricks.blogspot.com");
selenium.click("btnG");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
assertTrue(selenium.isTextPresent("http://automationtricks.blogspot.com"));
}
@AfterTest
public void tearDown() throws InterruptedException{
selenium.stop();
seleniumServer.stop();
}
}
Pages
Search This Blog
Wednesday, May 19, 2010
How to run test cases in specified browser profile
Selenium creates a new and clean profile each time it runs a test, so it won't keep any cookies of the previous session and it wont use any browser plug-in however you can force selenium to use your own profile.
Steps
1. Close all instances of firefox browser.
2. Create a new firefox profile : Go to Run and type firefox -p and press OK . This will open a dilog bod where you create new firefox profile, have a name "nk" and save it in C: drive.
3. Start the browser: Go to run and type firefox -p that will open a dialog box. Select the profile "NK" which you created in the 2nd step.
4. Install all plug-ins in this profile ; like SSL security bypass
https://addons.mozilla.org/en-US/firefox/addon/10246/
5. Now you write you test cases like below
Steps
1. Close all instances of firefox browser.
2. Create a new firefox profile : Go to Run and type firefox -p and press OK . This will open a dilog bod where you create new firefox profile, have a name "nk" and save it in C: drive.
3. Start the browser: Go to run and type firefox -p that will open a dialog box. Select the profile "NK" which you created in the 2nd step.
4. Install all plug-ins in this profile ; like SSL security bypass
https://addons.mozilla.org/en-US/firefox/addon/10246/
5. Now you write you test cases like below
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
import java.io.File;
public class firefoxprofile extends SeleneseTestCase {
private SeleniumServer seleniumServer;
private Selenium selenium;
public static final String MAX_WAIT_TIME_IN_MS="60000";
@BeforeClass
public void setUp() throws Exception {
File template = new File("C:\\nk");
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setSingleWindow(true);
rc.setFirefoxProfileTemplate(template);
seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/");
seleniumServer.start();
selenium.start();
}
@Test
public void googling() {
selenium.open("/");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
selenium.type("q", "http://automationtricks.blogspot.com");
selenium.click("btnG");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
assertTrue(selenium.isTextPresent("http://automationtricks.blogspot.com"));
}
@AfterTest
public void tearDown() throws InterruptedException{
selenium.stop();
seleniumServer.stop();
}
}
Tuesday, May 18, 2010
How to identify dynamic element in selenium
Many web sites create dynamic element on their web pages where Ids of the elements gets generated dynamically. Each time id gets generated differently. So to handle this situation we use some JavaScript functions.
starts-with
if your dynamic element's ids have the format <button id="continue-12345" /> where 12345 is a dynamic number you could use the following
Sometimes an element gets identfied by a value that could be surrounded by other text, then contains function can be used.
To demonstrate, the element <input class="top suggest business"> can be located based on the ‘suggest’ class without having to couple it with the ‘top’ and ‘business’ classes using the following
starts-with
if your dynamic element's ids have the format <button id="continue-12345" /> where 12345 is a dynamic number you could use the following
XPath: //button[starts-with(@id, 'continue-')]
contains Sometimes an element gets identfied by a value that could be surrounded by other text, then contains function can be used.
To demonstrate, the element <input class="top suggest business"> can be located based on the ‘suggest’ class without having to couple it with the ‘top’ and ‘business’ classes using the following
XPath: //input[contains(@class, 'suggest')].
How to use Css instead of Xpath in selenium
Sometimes selenium gets confused to identify an element on the web page because more than one elements have the same name and ids so to handle this situation we instructs selenium to identify the object using CSS instead of Xpath.
Try below code for clicking on search button on www.google.com
<tr>
<td>open</td>
<td>http://economictimes.indiatimes.com/</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>css=input[style='width: 120px; font-size: 12px;']</td>
<td>Business</td>
</tr>
In selenium RC you can use below code
selenium.open("http://economictimes.indiatimes.com/");
selenium.type("css=input[style='width: 120px; font-size: 12px;']", "Business");
There is another way to use css instead of XpathTry below code for clicking on search button on www.google.com
<tr>
<td>type</td>
<td>q</td>
<td>www.automationtricks.blogspot.com</td>
</tr>
<tr>
<td>click</td>
<td>css=span.ds > span.lsbb > input.lsb</td>
<td></td>
</tr>
Monday, May 17, 2010
When selenium.waitForPageToLoad is not working
When selenium.waitForPageToLoad() is not working then we can use alternate solution.
boolean Condition = false;
for (int second = 0; second < 60; second++) {
try {
if ((selenium.isElementPresent("xpath of your element on which you want to click"))) {
selenium.click("xpath of your element on which you want to click");
Condition = true;
break;
}
}
catch (Exception ignore) {
}
pause(1000);
}
assertTrue(Condition);
How to setup Selenium RC with TestNG in eclipse
TestNG is a framework to work in JAVA. Junit and TestNG almost same on the surface but there are major differences between their frameworks and their nature of testing. JUnit is designed to do the unit testing, and it's doing it very well while the TestNG has designed to do the functionality testing at high level. So there are many features available in the TestNG that you will not find in JUnit.
TestNG Annotation
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the tag is run.
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the tag have run.
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@Parameters: Describes how to pass parameters to a @Test method.
Test case
goolgesearch.java
TestNG Suite
googlesearchsuite.xml
In this method you dont have to start your Selenium RC server through command line . The java code will start the server , run the test and stop the server.
Before you run the Test , make sure all path and build path are correct.
Install TestNG plug-in in your eclipse.
Set the java build path and pointing to RC sever.
Go to Java project and right click on it then select properties then select java build path then select library tab and select external jar pointing you selenium RC server.
Run the test case as TestNG test suite.
Go to googlesearch.java page right click on the page the click on Run as... and select the suite in the dialog box which you created above.
TestNG Annotation
@BeforeTest: The annotated method will be run before any test method belonging to the classes inside the
@AfterTest: The annotated method will be run after all the test methods belonging to the classes inside the
@BeforeClass: The annotated method will be run before the first test method in the current class is invoked.
@AfterClass: The annotated method will be run after all the test methods in the current class have been run.
@Parameters: Describes how to pass parameters to a @Test method.
Test case
goolgesearch.java
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import org.testng.annotations.*;
import com.thoughtworks.selenium.*;
public class googlesearch extends SeleneseTestCase{
Selenium selenium;
public static final String MAX_WAIT_TIME_IN_MS="60000";
private SeleniumServer seleniumServer;
@BeforeClass
@Parameters({"selenium.host","selenium.port","selenium.browser","selenium.url"})
public void setUp(String host, String port, String browser , String url ) throws Exception {
RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setSingleWindow(true);
seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium(host, Integer.parseInt(port), browser, url);
seleniumServer.start();
selenium.start();
}
@Test
@Parameters({"search","expected"})
public void googling(String search, String expected) {
selenium.open("/");
selenium.waitForPageToLoad("6000");
selenium.type("q", search);
selenium.click("btnG");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
assertTrue(selenium.isTextPresent(expected));
}
@AfterTest
public void tearDown() throws InterruptedException{
selenium.stop();
seleniumServer.stop();
}
}
TestNG Suite
googlesearchsuite.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="googlesearch" verbose="3">
<parameter name="selenium.host" value="localhost"></parameter>
<parameter name="selenium.port" value="4444"></parameter>
<parameter name="selenium.browser" value="*iexplore"></parameter>
<parameter name="selenium.url" value="http://www.google.com"></parameter>
<test name="googlesearch">
<parameter name="search" value="automationtricks.blogspot.com"></parameter>
<parameter name="expected" value="automationtricks.blogspot.com"></parameter>
<classes>
<class name="googlesearch"></class>
</classes>
</test>
</suite>
In this method you dont have to start your Selenium RC server through command line . The java code will start the server , run the test and stop the server.
Before you run the Test , make sure all path and build path are correct.
Go to Java project and right click on it then select properties then select java build path then select library tab and select external jar pointing you selenium RC server.
Go to googlesearch.java page right click on the page the click on Run as... and select the suite in the dialog box which you created above.
Subscribe to:
Posts (Atom)