1+ package java_example ;
2+
3+ import java .net .MalformedURLException ;
4+ import java .net .URL ;
5+ import java .util .concurrent .TimeUnit ;
6+ import org .openqa .selenium .By ;
7+ import org .openqa .selenium .WebDriver ;
8+ import org .openqa .selenium .WebElement ;
9+ import org .openqa .selenium .remote .DesiredCapabilities ;
10+ import org .openqa .selenium .remote .RemoteWebDriver ;
11+ import org .testng .annotations .AfterTest ;
12+ import org .testng .annotations .BeforeTest ;
13+ import org .testng .annotations .Parameters ;
14+ import org .testng .annotations .Test ;
15+
16+ public class TestChrome {
17+ public WebDriver driver ;
18+ public String URL , Node ;
19+ protected ThreadLocal <RemoteWebDriver > threadDriver = null ;
20+
21+ @ Parameters (value ={"url" ,"seleniumGridUrl" })
22+ @ BeforeTest
23+ public void launchbrowser (String url , String seleniumGridUrl ) throws MalformedURLException {
24+ // String URL = "http://www.calculator.net";
25+ String browser = "chrome" ;
26+ String URL = url ;
27+ if (browser .equalsIgnoreCase ("firefox" )) {
28+ System .out .println (" Executing on FireFox at grid " + seleniumGridUrl );
29+ // String Node = "http://192.168.99.100:32768/wd/hub";
30+ String Node = seleniumGridUrl ;
31+ DesiredCapabilities cap = DesiredCapabilities .firefox ();
32+ cap .setBrowserName ("firefox" );
33+ driver = new RemoteWebDriver (new URL (Node ), cap );
34+ // Puts an Implicit wait, Will wait for 10 seconds before throwing
35+ // exception
36+ driver .manage ().timeouts ().implicitlyWait (10 , TimeUnit .SECONDS );
37+ // Launch website
38+ driver .navigate ().to (URL );
39+ driver .manage ().window ().maximize ();
40+ } else if (browser .equalsIgnoreCase ("chrome" )) {
41+ System .out .println (" Executing on CHROME at grid " + seleniumGridUrl );
42+ DesiredCapabilities cap = DesiredCapabilities .chrome ();
43+ cap .setBrowserName ("chrome" );
44+ // String Node = "http://192.168.99.100:32768/wd/hub";
45+ String Node = seleniumGridUrl ;
46+ driver = new RemoteWebDriver (new URL (Node ), cap );
47+ driver .manage ().timeouts ().implicitlyWait (10 , TimeUnit .SECONDS );
48+ // Launch website
49+ driver .navigate ().to (URL );
50+ driver .manage ().window ().maximize ();
51+ } else if (browser .equalsIgnoreCase ("ie" )) {
52+ System .out .println (" Executing on IE at grid " +seleniumGridUrl );
53+ DesiredCapabilities cap = DesiredCapabilities .chrome ();
54+ cap .setBrowserName ("ie" );
55+ // String Node = "http://192.168.99.100:32768/wd/hub";
56+ String Node = seleniumGridUrl ;
57+ driver = new RemoteWebDriver (new URL (Node ), cap );
58+ driver .manage ().timeouts ().implicitlyWait (10 , TimeUnit .SECONDS );
59+ // Launch website
60+ driver .navigate ().to (URL );
61+ driver .manage ().window ().maximize ();
62+ } else {
63+ throw new IllegalArgumentException ("The Browser Type is Undefined" );
64+ }
65+
66+ System .out .println ("Remote session Id: " + ((RemoteWebDriver ) driver ).getSessionId ());
67+ }
68+
69+ @ Test
70+ public void calculatepercent () {
71+ System .out .println ("Running the calculate percent test on chrome" );
72+ // Click on Math Calculators
73+ driver .findElement (By .xpath ("//a[contains(text(),'Math')]" )).click ();
74+ // Click on Percent Calculators
75+ driver .findElement (By .xpath ("//a[contains(text(),'Percentage Calculator')]" )).click ();
76+ // Enter value 17 in the first number of the percent Calculator
77+ driver .findElement (By .id ("cpar1" )).sendKeys ("17" );
78+ // Enter value 35 in the second number of the percent Calculator
79+ driver .findElement (By .id ("cpar2" )).sendKeys ("35" );
80+ // Click Calculate Button
81+ driver .findElement (By .xpath ("(//input[contains(@value,'Calculate')])[1]" )).click ();
82+ // Get the Result Text based on its xpath
83+ String result = driver .findElement (By .xpath (".//*[@id='content']/p[2]/font/b" )).getText ();
84+ // Print a Log In message to the screen
85+ System .out .println (" The Result is " + result );
86+ if (result .equals ("5.95" )) {
87+ System .out .println (" The Result is Pass" );
88+ } else {
89+ System .out .println (" The Result is Fail" );
90+ }
91+ }
92+
93+ @ Test
94+ public void open_python_site () throws Exception {
95+ System .out .println ("Running the open python doc test on chrome" );
96+ driver .get ("http://www.python.org" );
97+ Thread .sleep (10000 ); //slow down for demo purposes
98+ WebElement element = driver .findElement (By .name ("q" ));
99+ element .sendKeys ("documentation" );
100+ //elem.send_keys(Keys.RETURN)
101+ element .submit ();
102+ Thread .sleep (5000 ); //slow down for demo purposes
103+ }
104+
105+ @ AfterTest
106+ public void closeBrowser () {
107+ driver .quit ();
108+ }
109+ }
0 commit comments