|
| 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 | +} |
0 commit comments