вторник, 26 ноября 2013 г.

First WebDriver Script

Using the Java class “myclass”  that we created in the previous tutorial, let us try to create a WebDriver script that would:

  1. fetch Mercury Tours’ homepage
  2. verify its title
  3. print out the result of the comparison
  4. close it before ending the entire program.

WebDriver Code

Below is the actual WebDriver code for the logic presented by the scenario above
01.package mypackage;
02. 
03. 
04. 
05.import org.openqa.selenium.WebDriver;
06. 
07.import org.openqa.selenium.firefox.FirefoxDriver;
08. 
09. 
10. 
11.public class myclass {
12. 
13. 
14. 
15.public static void main(String[] args) {
16. 
17.// declaration and instantiation of objects/variables
18. 
19.WebDriver driver = new FirefoxDriver();
20. 
21.String baseUrl = "http://newtours.demoaut.com";
22. 
23.String expectedTitle = "Welcome: Mercury Tours";
24. 
25.String actualTitle = "";
26. 
27. 
28. 
29.// launch Firefox and direct it to the Base URL
30. 
31.driver.get(baseUrl);
32. 
33. 
34. 
35.// get the actual value of the title
36. 
37.actualTitle = driver.getTitle();
38. 
39. 
40. 
41./*
42. 
43.* compare the actual title of the page witht the expected one and print
44. 
45.* the result as "Passed" or "Failed"
46. 
47.*/
48. 
49.if (actualTitle.contentEquals(expectedTitle)){
50. 
51.System.out.println("Test Passed!");
52. 
53.else {
54. 
55.System.out.println("Test Failed");
56. 
57.}
58. 
59. 
60. 
61.//close Firefox
62. 
63.driver.close();
64. 
65. 
66. 
67.// exit the program explicitly
68. 
69.System.exit(0);
70. 
71.}
72. 
73. 
74. 
75.}