Pages

Search This Blog

Tuesday, August 24, 2010

How to handle Selenium is already running on port 4444

When we run selenium RC test cases sometimes we faces this issue saying "java.net.BindException: Selenium is already running on port 4444. Or some other service is." When you check the port 4444 no service is running. We change the port and run the program even that too is not working.
In these cases we need to shutdown the selenium server on this port.
Use below command to shut down the server.

http://localhost:4444/selenium-server/driver/?cmd=shutDownSeleniumServer

If selenium server is already running on port 4444 then it will shut down the server and says
OKOK
if selenium is not running on this port 4444 then by hitting above url will give you
"Unable to connect"
Now you can run your test cases i am sure will run smoothing.

How to get browser name, version and operating system detail in selenium

We can get the browser name, version and operating system name and version with the following commands in selenium RC.
Below code is tested in Mozilla Firefox
Browser Name

System.out.println(selenium.getEval("navigator.appCodeName;"));

Mozilla

Browser version

System.out.println(selenium.getEval("navigator.appVersion;"));

5.0 (Windows; en-US)

Browser and Operating system detail

System.out.println(selenium.getEval("navigator.userAgent;"));

Result.

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 (.NET CLR 3.5.30729)

String browser = selenium.getEval("navigator.userAgent");
if (browser.contains("Firefox")  || browser.contains("Chrome")) {


Thursday, August 19, 2010

How to verify html source in selenium

We can verify the html source with selenium commands

<html>
<head>
<title>HTML Source</title>
</head>
<body>
what need to be verifed Text is here we need to verify
</body>
</html>

Script


verifyTrue(selenium.getHtmlSource().matches("^[\\s\\S]*Text is here[\\s\\S]*$"));

above command will verify Text is here in above html srource

verifyTrue(selenium.getHtmlSource().matches("^[\\s\\S]*Text i here[\\s\\S]*$"));

above command will fail because it tries to verify Text i here which is not there in html source

How to handle the input promt dialog in selenium

There are some scenarios where we need to input when our web application need external input data by opening input prompt dialog box. To handle these dialog boxes in automation is tricky task. We can handle these situation with below codes.
Html file is at the bottom

selenium.open("Input_prompt_TestCase.html");

Verify there is no input dilog present while opening this file


assertTrue(!selenium.isPromptPresent());

When then Input prompts select NO first time by the command answerOnNextPrompt|no|

selenium.answerOnNextPrompt("no");

Now click to open the input dialog by clicking the link "Click here to open input dialog"

selenium.click("promptdialog");

Now verify prompt presents or not with the command verifyPromptPresent||
assertTrue(selenium.isPromptPresent());
If it takes time to open the prompt then we can wait till 1 minute with the below code.

boolean Bool = false;
for (int second = 0; second < 60; second++) { 
try { 
if ((selenium.isPromptPresent())) {
Bool = true;
break; 
}
}
catch (Exception ignore) {
}
pause(1000);
}
assertTrue(Bool);


Now after 1 minute verify the prompt presents or not with the command assertPromptPresent||


assertTrue(selenium.isPromptPresent());

If it verifies prompt is there then verify the text what is on prompt with the command
verifyPrompt|Type 'automationtricks' and click OK|


verifyEquals("Type 'automationtricks' and click OK", selenium.getPrompt());

You can also verify the title of input prompt with the command verifyTitle|Test Prompt|


verifyEquals("*Testcases for input Prompt", selenium.getTitle());

Now you just enter automationtricks in input promt with this command
answerOnNextPrompt|automationtricks|


selenium.answerOnNextPrompt("automationtricks");

and then click on ok with command clickAndWait|promptdialog|


selenium.click("promptdialog");

Now you can see the result based on your input


selenium.waitForPageToLoad("5000");
verifyTrue(selenium.isTextPresent("You have entered automationtricks!"));



For Selenium IDE user : How to handle prompt in selenium IDE


<tr>
<td>open</td>
<td>Input_prompt_TestCase.html</td>
<td></td>
</tr>
<tr>
<td>answerOnNextPrompt</td>
<td>automationtricks</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>promptdialog</td>
<td></td>
</tr>
<tr>
<td>verifyTextPresent</td>
<td>You have entered automationtricks!</td>
<td></td>
</tr>
<tr>
<td>open</td>
<td>Input_prompt_TestCase.html</td>
<td></td>
</tr>
<tr>
<td>answerOnNextPrompt</td>
<td>niraj</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>promptdialog</td>
<td></td>
</tr>
<tr>
<td>verifyTextPresent</td>
<td>You have not entered automationtricks!</td>
<td></td>
</tr>


For the above code find Html file below


<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html">
<script type="text/javascript">
function opendialog() {
if (prompt("Type 'automationtricks' and click OK") == 'automationtricks') {
document.write("You have entered automationtricks!");
}
else{
document.write("You have not entered automationtricks!");
}
}
</script>
<title>Testcases for input Prompt</title>
</head>
<body>
<img style="width: 644px; height: 41px;" alt="banner" src="banner.gif"><br>
<a id="promptdialog" href="javascript:opendialog();">Click here to open input dialog</a>
</body>
</html>


Wednesday, August 18, 2010

How to create test suite using Junit and eclipse in selenium

There are some scenarios where we need to run multiple test cases. Either we can run those test cases independently or together. But there are some real time cases where we need to run our test cases in a particular order. In this case we would prefer Test Suite to combine the test cases together and decide their orders and run those.


Below are the steps

1. Create a Test Suite class where we create the Test Suites which will call all the test cases in a particular order.

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;  
import junit.textui.TestRunner;

public class TestSuite1 extends TestCase {

public static Test suite()  
{  
TestSuite suite = new TestSuite();  

suite.addTestSuite( TestCase1.class);  
suite.addTestSuite( TestCase2.class);
suite.addTestSuite( TestCase3.class);  
return suite;  
}  

public static void main(String arg[])
{
TestRunner.run(suite());

}
}


Step 2. Create your first test case

import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.*;
public class TestCase1 extends SeleneseTestCase{

Selenium selenium;
public static final String MAX_WAIT_TIME_IN_MS="60000";
private SeleniumServer seleniumServer;

public void setUp() throws Exception {

RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setSingleWindow(true);
seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.com/");
seleniumServer.start();
selenium.start();

}
public void testgoogling() {
selenium.open("/");
selenium.type("q", "Niraj");
selenium.click("btnG");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
assertTrue(selenium.isTextPresent("Niraj"));

}

public void tearDown() throws InterruptedException{
selenium.stop(); 
seleniumServer.stop();
}
}

Step 3. Create your second test case

import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.*;
public class TestCase2 extends SeleneseTestCase{
Selenium selenium;
public static final String MAX_WAIT_TIME_IN_MS="60000";
private SeleniumServer seleniumServer;
public void setUp() throws Exception {

RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setSingleWindow(true);
seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.com/");
seleniumServer.start();
selenium.start();
}

public void testgoogling() {
selenium.open("/");
selenium.type("q", "Niraj Kumar");
selenium.click("btnG");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
assertTrue(selenium.isTextPresent("Niraj Kumar"));

}

public void tearDown() throws InterruptedException{
selenium.stop(); 
seleniumServer.stop();
}
}


Step 4. Create your third test case

import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.*;
public class TestCase3 extends SeleneseTestCase{
Selenium selenium;
public static final String MAX_WAIT_TIME_IN_MS="60000";
private SeleniumServer seleniumServer;
public void setUp() throws Exception {

RemoteControlConfiguration rc = new RemoteControlConfiguration();
rc.setSingleWindow(true);
seleniumServer = new SeleniumServer(rc);
selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.google.com/");
seleniumServer.start();
selenium.start();
}

public void testgoogling() {
selenium.open("/");
selenium.type("q", "http://www.automationtricks.blogspot.com");
selenium.click("btnG");
selenium.waitForPageToLoad(MAX_WAIT_TIME_IN_MS);
assertTrue(selenium.isTextPresent("http://www.automationtricks.blogspot.com"));

}

public void tearDown() throws InterruptedException{
selenium.stop(); 
seleniumServer.stop(); 
}
}

Step 5. Run your Test Suite
Go to your Test Suite class and right click on that and run as Junit Test.

This will run the TestCase1 then TestCase2 then TestCase3


If you want to execute your test cases in some specific order then you call them in that order like.

suite.addTestSuite( TestCase3.class);  
suite.addTestSuite( TestCase2.class);
suite.addTestSuite( TestCase1.class);  

Above will run the TestCase3 first then TestCase2 then TestCase1.