Skip to content

Commit 29c9d67

Browse files
author
Mat Walker
committed
Completed Click actions and tests - ready for first pull request
1 parent 2950733 commit 29c9d67

File tree

7 files changed

+519
-20
lines changed

7 files changed

+519
-20
lines changed

TestServer/chromedriver.exe

6.11 MB
Binary file not shown.
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package TeamControlium.Controlium.Test;
2+
3+
import TeamControlium.Controlium.*;
4+
import TeamControlium.Utilities.Logger;
5+
import TeamControlium.Utilities.TestData;
6+
import org.apache.commons.lang3.time.StopWatch;
7+
import org.junit.jupiter.api.AfterEach;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Assertions;
10+
11+
import java.io.BufferedReader;
12+
import java.io.InputStreamReader;
13+
import java.time.Duration;
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
import java.util.concurrent.TimeUnit;
17+
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
public class BasicNavigationAndFindTests {
21+
22+
private SeleniumDriver seleniumDriver=null;
23+
@BeforeEach
24+
void setUp() {
25+
TestData.setItem("Selenium", "SeleniumServerFolder", "./TestServer");
26+
if (seleniumDriver!=null) seleniumDriver.CloseDriver();
27+
seleniumDriver = new SeleniumDriver(true);
28+
29+
}
30+
31+
@AfterEach
32+
void tearDown() {
33+
if (seleniumDriver!=null) seleniumDriver.CloseDriver();
34+
}
35+
36+
// Verify we can browse to a URL with Selenium
37+
@org.junit.jupiter.api.Test
38+
void VerifyBrowseToWorks() {
39+
seleniumDriver.gotoURL("http://www.google.com");
40+
41+
String pageTitle = seleniumDriver.getPageTitle();
42+
Assertions.assertEquals("Google", pageTitle, "Page title correct");
43+
}
44+
45+
// Verify we can find an element
46+
@org.junit.jupiter.api.Test
47+
void VerifyBasicFindElement() {
48+
seleniumDriver.gotoURL("http://www.google.com");
49+
HTMLElement element=null;
50+
try {
51+
element = seleniumDriver.findElement(new ObjectMapping("//input[@id='lst-ib']"));
52+
}
53+
catch( Exception e) {
54+
Logger.WriteLine(Logger.LogLevels.TestInformation,"Error thrown finding Google search testbox element: ",e.getMessage());
55+
}
56+
assertNotNull(element,"Returned element not null");
57+
}
58+
59+
// Verify timeout when finding non-existant element
60+
@org.junit.jupiter.api.Test
61+
void VerifyFindElementTimeout() {
62+
seleniumDriver.setFindTimeout(Duration.ofMillis(10000));
63+
seleniumDriver.gotoURL("http://www.google.com");
64+
HTMLElement element=null;
65+
StopWatch stopWatch = StopWatch.createStarted();
66+
try {
67+
element = seleniumDriver.findElement(new ObjectMapping("//input[@id='wont match']"));
68+
}
69+
catch( Exception e) {
70+
stopWatch.stop();
71+
Logger.WriteLine(Logger.LogLevels.TestInformation,"Error thrown finding Google search testbox element: ",e.getMessage());
72+
}
73+
74+
int actualTime = (int)stopWatch.getTime(TimeUnit.SECONDS);
75+
76+
assertEquals(true,(actualTime>9 && actualTime<12), String.format("Timeout ([%d]) approx 10 seconds",actualTime));
77+
assertNull(element,"Returned element null");
78+
}
79+
80+
// Verify error after correct timeout when requiring single element but find logic returns multiple
81+
@org.junit.jupiter.api.Test
82+
void VerifyFindElementFailForMultiple() {
83+
seleniumDriver.setFindTimeout(Duration.ofMillis(10000));
84+
seleniumDriver.gotoURL("http://www.google.com");
85+
HTMLElement element=null;
86+
String errorMessage=null;
87+
StopWatch stopWatch = StopWatch.createStarted();
88+
try {
89+
element = seleniumDriver.findElement(new ObjectMapping("//input","Find logic returning mutiple elements"),true,false);
90+
}
91+
catch( Exception e) {
92+
stopWatch.stop();
93+
errorMessage=e.getMessage();
94+
}
95+
96+
int actualTime = (int)stopWatch.getTime(TimeUnit.SECONDS);
97+
98+
assertEquals(true,(actualTime>9 && actualTime<12), String.format("Timeout ([%d]) approx 10 seconds",actualTime));
99+
assertNull(element,"Returned element null");
100+
assertEquals(true,(errorMessage!=null && errorMessage.startsWith("Found 9 matching elements using [//input] ([Find logic returning mutiple elements]) from [DOM Top Level]. Not allowing mutiple matches and timeout reached after")),"Correct error message");
101+
}
102+
103+
// Verify we can find multiple elements
104+
@org.junit.jupiter.api.Test
105+
void VerifyBasicFindMultipleElements() {
106+
seleniumDriver.gotoURL("http://www.google.com");
107+
List<HTMLElement> elements=null;
108+
try {
109+
elements = seleniumDriver.findElements(null,new ObjectMapping("//input"));
110+
}
111+
catch( Exception e) {
112+
Logger.WriteLine(Logger.LogLevels.TestInformation,"Error thrown finding Google search testbox element: ",e.getMessage());
113+
}
114+
assertEquals(9,elements.size(),"Verify 9 input elements on Google search site");
115+
}
116+
117+
// Verify correct error message when find fails (No friendly name set)
118+
@org.junit.jupiter.api.Test
119+
void VerifyCorrectErrorWithBadFindLogicNoFriendlyName() {
120+
seleniumDriver.gotoURL("http://www.google.com");
121+
HTMLElement element=null;
122+
String errorMessage=null;
123+
try {
124+
element = seleniumDriver.findElement(new ObjectMapping("//wibble'"));
125+
}
126+
catch( Exception e) {
127+
errorMessage = e.getMessage();
128+
}
129+
assertNull(element,"No returned element");
130+
assertEquals("Selenium Driver error. Find Logic [//wibble'] for [//wibble'] is invalid!",errorMessage,"Correct error message");
131+
}
132+
133+
// Verify correct error message when find files (Friendly name set)
134+
@org.junit.jupiter.api.Test
135+
void VerifyCorrectErrorWithBadFindLogicWithFriendlyName() {
136+
seleniumDriver.gotoURL("http://www.google.com");
137+
HTMLElement element=null;
138+
String errorMessage=null;
139+
try {
140+
element = seleniumDriver.findElement(new ObjectMapping("//wibble'","Non-sense find logic"));
141+
}
142+
catch( Exception e) {
143+
errorMessage = e.getMessage();
144+
}
145+
assertNull(element,"No returned element");
146+
assertEquals("Selenium Driver error. Find Logic [//wibble'] for [Non-sense find logic] is invalid!",errorMessage,"Correct error message");
147+
}
148+
149+
// Verify find fails when needing element to be stable and it is not
150+
@org.junit.jupiter.api.Test
151+
void VerifyUnstableElementTimeout() {
152+
seleniumDriver.setFindTimeout(Duration.ofMillis(10000));
153+
seleniumDriver.gotoURL("http://bouncejs.com/");
154+
HTMLElement element=null;
155+
String errorMessage=null;
156+
StopWatch stopWatch = StopWatch.createStarted();
157+
try {
158+
element = seleniumDriver.findElement(new ObjectMapping("//div[@id='preferences']/div[@class='empty-message']","Moving element in left preferences panel"),true);
159+
}
160+
catch( Exception e) {
161+
stopWatch.stop();
162+
errorMessage = e.getMessage();
163+
}
164+
165+
int actualTime = (int)stopWatch.getTime(TimeUnit.SECONDS);
166+
167+
assertEquals(true,(actualTime>9 && actualTime<12), String.format("Timeout ([%d]) approx 10 seconds",actualTime));
168+
assertEquals(true,(errorMessage!=null && errorMessage.startsWith("From [DOM Top Level], find [//div[@id='preferences']/div[@class='empty-message'] (Moving element in left preferences panel)] returned 1 matches (Not Allowing multiple matches). Element NOT stable after timeout reached")),"Correct Error message");
169+
assertNull(element,"No returned element");
170+
}
171+
172+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package TeamControlium.Controlium.Test;
2+
3+
import TeamControlium.Controlium.*;
4+
import TeamControlium.Controlium.Exception.InvalidElementState;
5+
import TeamControlium.Utilities.Logger;
6+
import TeamControlium.Utilities.TestData;
7+
import org.apache.commons.lang3.time.StopWatch;
8+
import org.junit.jupiter.api.AfterEach;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Assertions;
11+
12+
import java.io.BufferedReader;
13+
import java.io.InputStreamReader;
14+
import java.time.Duration;
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
import java.util.concurrent.TimeUnit;
18+
19+
import static org.junit.jupiter.api.Assertions.*;
20+
21+
public class ElementInteractionTests {
22+
23+
private SeleniumDriver seleniumDriver=null;
24+
@BeforeEach
25+
void setUp() {
26+
TestData.setItem("Selenium", "SeleniumServerFolder", "./TestServer");
27+
if (seleniumDriver!=null) seleniumDriver.CloseDriver();
28+
seleniumDriver = new SeleniumDriver(true);
29+
30+
}
31+
32+
@AfterEach
33+
void tearDown() {
34+
if (seleniumDriver!=null) seleniumDriver.CloseDriver();
35+
}
36+
37+
// Verify we can enter and readback text
38+
@org.junit.jupiter.api.Test
39+
void VerifyDataEntryAndReadBack() {
40+
String testText = "My entered text";
41+
seleniumDriver.gotoURL("https://www.w3schools.com/angular/tryit.asp?filename=try_ng_example1");
42+
HTMLElement inputElement=null;
43+
HTMLElement outputElement=null;
44+
try {
45+
HTMLElement iframe = seleniumDriver.findElement(new ObjectMapping("//iframe[@id='iframeResult']"));
46+
seleniumDriver.setIFrame(iframe);
47+
inputElement = seleniumDriver.findElement(new ObjectMapping("//input[@ng-model='name']"));
48+
outputElement = seleniumDriver.findElement(new ObjectMapping("//p[@class='ng-binding' and starts-with(.,'You wrote')]"));
49+
}
50+
catch( Exception e) {
51+
Logger.WriteLine(Logger.LogLevels.TestInformation,"Error thrown finding textbox element: ",e.getMessage());
52+
assertNotNull(inputElement,"Returned element must not be null!!");
53+
}
54+
55+
inputElement.setText(testText);
56+
String textBack = outputElement.getText();
57+
58+
assertEquals(true,textBack.contains("My entered text"),String.format("Output label contains entered text [%s]",testText));
59+
}
60+
61+
// Verify we can enter and readback text
62+
@org.junit.jupiter.api.Test
63+
void CheckClickOverlayProcessing() {
64+
String testText = "My entered text";
65+
ObjectMapping overlayButtonFindLogic = new ObjectMapping("//button[.='Turn on the overlay effect']", "Overlay effect button");
66+
67+
final HTMLElement overlayButton;
68+
seleniumDriver.gotoURL("https://www.w3schools.com/howto/howto_css_overlay.asp");
69+
70+
// Find the button
71+
try {
72+
overlayButton = seleniumDriver.findElement(overlayButtonFindLogic);
73+
} catch (Exception e) {
74+
Logger.WriteLine(Logger.LogLevels.TestInformation, "Error thrown finding [%s]: %s", overlayButtonFindLogic.getFriendlyName(), e.getMessage());
75+
throw new RuntimeException(String.format("Error thrown finding [%s]", overlayButtonFindLogic.getFriendlyName()), e);
76+
}
77+
78+
// Click it
79+
try {
80+
overlayButton.click();
81+
} catch (Exception e) {
82+
Logger.WriteLine(Logger.LogLevels.TestInformation, "Error thrown clicking [%s]: %s", overlayButton.getFriendlyName(), e.getMessage());
83+
throw new RuntimeException(String.format("Error thrown clicking [%s]", overlayButton.getFriendlyName()), e);
84+
}
85+
86+
//
87+
// We now have the overlay shown, so lets try clicking it again.... :-)
88+
//
89+
90+
//
91+
// Check correct exception when we try clicking
92+
//
93+
Exception exceptionThrown=null;
94+
try {
95+
overlayButton.click();
96+
}
97+
catch (Exception ex) {
98+
exceptionThrown=ex;
99+
}
100+
101+
// Check we have the right exception thrown
102+
assertEquals(InvalidElementState.class, exceptionThrown.getClass());
103+
104+
// And make sure the correct offending element was identified
105+
assertEquals(true,exceptionThrown.getMessage().contains("div id=\"overlay\""),"Cause string identifies correct element");
106+
}
107+
108+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package TeamControlium.Controlium.Test;
2+
3+
import TeamControlium.Controlium.*;
4+
import TeamControlium.Utilities.TestData;
5+
import org.junit.jupiter.api.AfterEach;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Assertions;
8+
9+
import java.io.BufferedReader;
10+
import java.io.InputStreamReader;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
import static org.junit.jupiter.api.Assertions.*;
15+
16+
public class SeleniumServerTests {
17+
18+
@BeforeEach
19+
void setUp() {
20+
TestData.setItem("Selenium", "SeleniumServerFolder","./TestServer");
21+
}
22+
23+
@AfterEach
24+
void tearDown() {
25+
}
26+
27+
// Verify Selenum can be launched locally
28+
@org.junit.jupiter.api.Test
29+
void VerifySeleniumDriverLaunchesChromeAsDefault() {
30+
int chromeCountBefore=0;
31+
int chromeCountAfter=0;
32+
for (String[] line: getProcessList()) {
33+
if (line[0].equalsIgnoreCase("chromedriver.exe")) chromeCountBefore++;
34+
}
35+
36+
SeleniumDriver seleniumDriver = new SeleniumDriver(false);
37+
38+
try {Thread.sleep(2000);} catch(Exception e) {}
39+
for (String[] line: getProcessList()) {
40+
if (line[0].equalsIgnoreCase("chromedriver.exe")) chromeCountAfter++;
41+
}
42+
Assertions.assertEquals(chromeCountBefore,chromeCountAfter-1,"Number of Chrome Driver instances increments");
43+
}
44+
45+
// Verify Selenium can kill other instances of Server on startup
46+
@org.junit.jupiter.api.Test
47+
void VerifySeleniumDriverCanKillPreviousInstances() {
48+
int chromeCountBefore=0;
49+
int chromeCountAfter=0;
50+
for (String[] line: getProcessList()) {
51+
if (line[0].equalsIgnoreCase("chromedriver.exe")) chromeCountBefore++;
52+
}
53+
54+
SeleniumDriver seleniumDriver1 = new SeleniumDriver(false);
55+
56+
SeleniumDriver seleniumDriver2 = new SeleniumDriver(true);
57+
58+
try {Thread.sleep(2000);} catch(Exception e) {}
59+
60+
for (String[] line: getProcessList()) {
61+
if (line[0].equalsIgnoreCase("chromedriver.exe")) chromeCountAfter++;
62+
}
63+
Assertions.assertEquals(1,chromeCountAfter,"Number of Chrome Driver instances exactly 1");
64+
65+
seleniumDriver1.CloseDriver();
66+
seleniumDriver2.CloseDriver();
67+
}
68+
69+
70+
private List<String[]> getProcessList() {
71+
boolean startLogging = false;
72+
// MAT GET THIS WORKING - Driver executable filesnames.........
73+
74+
List<String[]> list = new ArrayList<String[]>();
75+
try {
76+
String line;
77+
//Process p = Runtime.getRuntime().exec("ps -e");
78+
Process p = Runtime.getRuntime().exec (System.getenv("windir") +"\\system32\\"+"tasklist.exe");
79+
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
80+
while ((line = input.readLine()) != null) {
81+
String[] arrayLine = line.split("\\s+");
82+
if (startLogging) list.add(arrayLine);
83+
if (line.contains("=======")) startLogging=true;
84+
}
85+
input.close();
86+
} catch (Exception err) {Assertions.fail("Error getting process list: ",err);
87+
}
88+
return list;
89+
}
90+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package TeamControlium.Controlium.Exception;
2+
3+
public class InvalidElementState extends RuntimeException{
4+
5+
public InvalidElementState() {}
6+
7+
public InvalidElementState(String message, RuntimeException innerException) {
8+
super(message,innerException);
9+
}
10+
11+
public InvalidElementState(String message) {
12+
super(message);
13+
}
14+
15+
}

0 commit comments

Comments
 (0)