First WebDriver
Script
With the help of Java
class ‘myclass’ that you developed in the earlier tutorial, So Now need to
try to develop a WebDriver scripts that would:
1.
Bring Mercury Tours
home page
2.
Confirm its title
3.
Print out the Output
for related comparisons
4.
Close it before
finishing the complete programs.
Code of WebDriver
Below is
the real code of WebDriver for the logic presented by the above scenario.
01.package
mypackage;
05.import
org.openqa.selenium.WebDriver;
07.import
org.openqa.selenium.firefox.FirefoxDriver;
11.public
class
myclass {
15.public
static
void
main(String[] args) {
17. // declaration
and the instantiation of objects / variables
19. WebDriver
driver = new
FirefoxDriver();
21. String baseUrl
= "http://newtours.demoprvd.com";
23. String
expectedTitle = "Welcome All Of You: Mercury Tour";
25. String
actualTitle = "";
29. // launch
Firefox and direct it to the Base URL
31. driver.get(baseUrl);
35. // get the real
value of the title
37. actualTitle=driver.getTitle();
41. /*
43. * compare
the actual title of the page with the expected
one and print
45. * the
result either "Passed" or "Failed"
47. */
49. if
(actualTitle.contentEquals(expectedTitle)){
51. System.out.println("Test
Passed!");
53. } else
{
55. System.out.println("Test
Failed");
57. }
61. //close Firefox
63. driver.close();
67. // come out the
program explicitly
69. System.exit(0);
71. }
75.}
Code Explanations:
Packages Importing - For starting we require to import packages given below:
- org.openqa.selenium.*- include the classes of WebDriver required to instantiate a newly browser loaded with the particular or specific drivers.
- org.openqa.selenium.firefox.FirefoxDriver – includes the FirefoxDriver classes required to instantiate the Firefox - specific drivers onto the browsers instantiated by the WebDriver classes.
If our test requires more difficult actions such as
accessing other classes, taking browsers screenshot, or manipulating the
external file, absolutely we will require importing more packages.
Instantiating objects and variables
Usually, this is the how
any driver’s objects are instantiated.
WebDriver drivers = new Firefoxdriver();
A FirefoxDriver classes
without parameters, It stands for that the default Firefox profiles will be started
by your Java programs. The default Firefox profiles are parallel to starting
the Firefox in the safe mode (non extension is loaded).
For more convenience, we stored
the Base URL and the estimated title as variable.
Launching the Browsers Sessions
WebDriver get()
function is utilized to starts the new
session of browsers and direct it to the URL that we identify as its parameters.
Driver.get(baseUrl);
Obtain the Title of Actual Page
The WebDriver classes have
the getTitle() function that is always utilized to get the title of pages of the presently loaded pages.
actualTitle = driver.getTitle();
Comparing the Expected with Actual Values
This part of code simply utilizes
the basic Java if - else structures to compared the actual or real title with
the expected result.
If(actualTitle.contentEquals(expctedTitle))
{
System.out.printin(“Testing Pass”);
} else {
System.out.printIn(“Testing Fail”);
Browser Session Terminating
The “close()”
function is utilized to closed the window of browser.
Driver.close();
Entire Program Terminating
If we utilize
such commands without closing windows of all browsers first, our whole Java
program will get end, while leaving the window of browser open.
System.exit(0);
Running the Test
We have two ways to run
the code in Eclipse IDE.
1.
From menu bar of
Eclipse, click on Run -> Run.
2.
Now Press the Ctrl
+ F11 to execute the complete code.
First WebDriver Script |
If we
did all fastly and without any interruption then Eclipse would display the output
“Test Passed!”
Locating GUI Elements
Locating the elements in
WebDriver is completed by utilizing the “findElement(By.locator())”
function. The part of the code “locator” is similar as any of the locators
past discussed under the Selenium IDE chapter of such tutoriasl.Infact, it is
the recommended, we locate GUI element utilizing the IDE and once identified
successfully exported the code to the webdriver.
Here is the sample code
that locates elements by its id. Facebok is utilized as the Base URL.
01.package
mypackage;
02.
03.import
org.openqa.selenium.By;
04.
05.import
org.openqa.selenium.WebDriver;
06.
07.import
org.openqa.selenium.firefox.FirefoxDriver;
08.
09.public
class
myclass {
10.
11.
12.
13.public
static
void
main(String[] args) {
14.
15.WebDriver driver = new
FirefoxDriver();
16.
17.String baseUrl = "http://www.facebok.com";
18.
19.String tagName = "";
20.
21.
22.
23.driver.get(baseUrl);
24.
25.tagName =
driver.findElement(By.id("email")).getTagName();
26.
27.System.out.println(tagsName);
28.
29.driver.close();
30.
31.System.exit(0);
32.
33.}
34.
35.}
You
utilized the getTagName() function to exclude the name of tag
of that specific elements whose id is “mail”. When execute, this code should be
capable to properly recognize the name of tag “input” and will print it out on
the Console window of Eclipse.
Read More about Web Driver Script
MOBILE APPS QTP TESTING QUESTIONS MANUAL TESTING ANDROID APPS
Read More about Web Driver Script
MOBILE APPS QTP TESTING QUESTIONS MANUAL TESTING ANDROID APPS
No comments:
Post a Comment