Friday, November 14, 2014
On 5:39 AM by Unknown in WebDriver Scripting No comments
Using the Java class “myclass” that we created previously, let us try to create a WebDriver script that would:
WebDriver Code
Below is the actual WebDriver code for the logic presented by the scenario above:
Explaining the code
Importing PackagesTo get started, you need to import following two packages:
org.openqa.selenium.*- contains the WebDriver class needed to instantiate a new browser loaded with a specific driverorg.openqa.selenium.firefox.FirefoxDriver – contains the FirefoxDriver class needed to instantiate a Firefox-specific driver onto the browser instantiated by the WebDriver classIf your test needs more complicated actions such as accessing another class, taking browser screenshots, or manipulating external files, definitely you will need to import more packages.
Instantiating objects and variables
Normally, this is how a driver object is instantiated.
A FirefoxDriver class with no parameters means that the default Firefox profile will be launched by our Java program. The default Firefox profile is similar to launching Firefox in safe mode (no extensions are loaded).
For convenience, we saved the Base URL and the expected title as variables.
Launching a Browser Session
WebDriver’s get() method is used to launch a new browser session and directs it to the URL that you specify as its parameter.
Get the Actual Page Title
The WebDriver class has the getTitle() method that is always used to obtain the page title of the currently loaded page.
Compare the Expected and Actual Values
This portion of the code simply uses a basic Java if-else structure to compare the actual title with the expected one.
Terminating a Browser Session
The “close()” method is used to close the browser window.
Terminating the Entire Program
If you use this command without closing all browser windows first, your whole Java program will end, while leaving browser window open.
Running the Test
There are two ways to execute code in Eclipse IDE.
On Eclipse’s menu bar, click Run > Run.
Press Ctrl+F11 to run the entire code.

If you did everything correctly, Eclipse would output “Test Passed!”
- Fetch Mercury Tours’ homepage
- Verify its title
- Print out the result of the comparison
- Close it before ending the entire program.
WebDriver Code
Below is the actual WebDriver code for the logic presented by the scenario above:
package mypackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class myclass {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://newtours.demoaut.com";
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = "";
// launch Firefox and direct it to the Base URL
driver.get(baseUrl);
// get the actual value of the title
actualTitle = driver.getTitle();
/*compare the actual title of the page with the expected one and print the result as "Passed" or "Failed"*/
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
//close Firefox
driver.close();
// exit the program explicitly
System.exit(0);
}
}
Explaining the code
Importing PackagesTo get started, you need to import following two packages:
org.openqa.selenium.*- contains the WebDriver class needed to instantiate a new browser loaded with a specific driverorg.openqa.selenium.firefox.FirefoxDriver – contains the FirefoxDriver class needed to instantiate a Firefox-specific driver onto the browser instantiated by the WebDriver classIf your test needs more complicated actions such as accessing another class, taking browser screenshots, or manipulating external files, definitely you will need to import more packages.
Instantiating objects and variables
Normally, this is how a driver object is instantiated.
WebDriver driver = new FirefoxDriver();
A FirefoxDriver class with no parameters means that the default Firefox profile will be launched by our Java program. The default Firefox profile is similar to launching Firefox in safe mode (no extensions are loaded).
For convenience, we saved the Base URL and the expected title as variables.
Launching a Browser Session
WebDriver’s get() method is used to launch a new browser session and directs it to the URL that you specify as its parameter.
driver.get(baseUrl);
Get the Actual Page Title
The WebDriver class has the getTitle() method that is always used to obtain the page title of the currently loaded page.
actualTitle = driver.getTitle();
Compare the Expected and Actual Values
This portion of the code simply uses a basic Java if-else structure to compare the actual title with the expected one.
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
Terminating a Browser Session
The “close()” method is used to close the browser window.
driver.close();
Terminating the Entire Program
If you use this command without closing all browser windows first, your whole Java program will end, while leaving browser window open.
System.exit(0);
Running the Test
There are two ways to execute code in Eclipse IDE.
On Eclipse’s menu bar, click Run > Run.
Press Ctrl+F11 to run the entire code.

If you did everything correctly, Eclipse would output “Test Passed!”

Subscribe to:
Post Comments (Atom)
Search
Popular Posts
-
Sample Selenium IDE Test Script by Recording: We will use the Mercury Tours website as our web application under test. It is an online flig...
-
Locating GUI Elements Locating elements in WebDriver is done by using the “findElement(By. locator ())” method. The “locator” part of the co...
-
Answer 75 : If you want to see all options available while starting Selenium server then you should use option “-h” while starting Seleni...
-
Answer 60 : While using java you need to create instance of DefaultSelenium class and pass it four parameters – selenium = ne...
-
Answer 51 : Selenium RC is an offering from SeleniumHQ which tries to overcome following draw backs of Selenium IDE – Able to execute...
-
Answer 54 : Selenium core is the core JS engine of Selenium which executes tests on browser, but because of same origin policy it needs t...
-
Answer 18 : You need to use context menu to add check points to your Selenium IDE tests Answer 19 : There are two ways to edit tests...
-
Answer 15 : Yes, you can first record individual test cases and then group all of them in a test suite. Following this entire test suite ...
-
Answer 38 : You could XPath checker - https://addons.mozilla.org/en-US/firefox/addon/xpath-checker/ to test you XPath locators and Fi...
-
Answer 44 : You can change default behavior of Selenium IDE > element locator preference by crating JS file with following– Locato...
Shaik. Powered by Blogger.
0 comments:
Post a Comment