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
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.
Like locators, patterns are a type of parameter frequently required by Selenese commands. Examples
of commands which require patterns are verifyTextPresent, verifyTitle, verifyAlert, assertConfirmation,
verifyText, and verifyPrompt.Patterns allow one to describe, via the use of special characters, what text is expected rather
than having to specify that text exactly
Global Patterns
* which translates to “match anything,” i.e., nothing, a single character, or many characters.
[ ] (character class) which translates to “match any single character found inside the square
brackets.” A dash (hyphen) can be used as a shorthand to specify a range of characters
(which are contiguous in the ASCII character set). A few examples will make the functionality
of a character class clear:
[aeiou] matches any lowercase vowel
[0-9] matches any digit
[a-zA-Z0-9] matches any alphanumeric character
In most other contexts, globbing includes a third special character, the ?. However, Selenium globbing
patterns only support the asterisk and character class.
To specify a globbing pattern parameter for a Selenese command, one can prefix the pattern with a glob:
label. However, because globbing patterns are the default, one can also omit the label and specify just
the pattern itself.
Below is an example of two commands that use globbing patterns. The actual link text on the page
being tested was “Film/Television Department”; by using a pattern rather than the exact text, the click
command will work even if the link text is changed to “Film & Television Department” or “Film and
Television Department”. The glob pattern’s asterisk will match “anything or nothing” between the word
“Film” and the word “Television”.
click link=glob:Film*Television Department
verifyTitle glob:*Film*Television*
The actual title of the page reached by clicking on the link was “De Anza Film And Television Department
- Menu”. By using a pattern rather than the exact text, the verifyTitle will pass as long as
the two words “Film” and “Television” appear (in that order) anywhere in the page’s title. For example,
if the page’s owner should shorten the title to just “Film & Television Department,” the test would still
pass. Using a pattern for both a link and a simple test that the link worked (such as the verifyTitle
above does) can greatly reduce the maintenance for such test cases.
Some examples for global pattern
Lets say your element looks like
<input class="foo" value="the text value" name="theText">
Regular expression
Regular expression patterns are the most powerful of the three types of patterns that Selenese supports.
Regular expressions are also supported by most high-level programming languages, many text editors and a host of tools, including the Linux/Unix command-line utilities grep, sed, and awk.
regexp (wildcard) matching
<input class="foo" value="the text value" name="theText">