|
| 1 | +package com.thoughtworks.selenium.corerunner; |
| 2 | + |
| 3 | +import com.google.common.collect.ImmutableMap; |
| 4 | + |
| 5 | +import com.thoughtworks.selenium.Selenium; |
| 6 | +import com.thoughtworks.selenium.SeleniumException; |
| 7 | + |
| 8 | +import org.openqa.selenium.WebDriver; |
| 9 | + |
| 10 | +import java.lang.reflect.Method; |
| 11 | +import java.util.HashSet; |
| 12 | +import java.util.Objects; |
| 13 | +import java.util.Set; |
| 14 | + |
| 15 | +public class CoreTestStep { |
| 16 | + |
| 17 | + private static ImmutableMap<String, SeleneseCommand> COMMANDS = buildCommands(); |
| 18 | + private final SeleneseCommand seleneseCommand; |
| 19 | + private final String locator; |
| 20 | + private final String value; |
| 21 | + |
| 22 | + public CoreTestStep(String command, String locator, String value) { |
| 23 | + seleneseCommand = COMMANDS.get(command); |
| 24 | + this.locator = locator; |
| 25 | + this.value = value; |
| 26 | + |
| 27 | + if (seleneseCommand == null) { |
| 28 | + throw new SeleniumException("Unknown command: " + command); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public void run(Results results, WebDriver driver, Selenium selenium) { |
| 33 | + seleneseCommand.execute(driver, selenium, locator, value); |
| 34 | + } |
| 35 | + |
| 36 | + private static ImmutableMap<String, SeleneseCommand> buildCommands() { |
| 37 | + ImmutableMap.Builder<String, SeleneseCommand> commands = ImmutableMap.builder(); |
| 38 | + Set<String> seenNames = new HashSet<>(); |
| 39 | + |
| 40 | + // seed the seen names with methods we definitely don't want folks accessing |
| 41 | + seenNames.add("addCustomRequestHeader"); |
| 42 | + seenNames.add("allowNativeXpath"); |
| 43 | + seenNames.add("pause"); |
| 44 | + seenNames.add("rollup"); |
| 45 | + seenNames.add("setBrowserLogLevel"); |
| 46 | + seenNames.add("setExtensionJs"); |
| 47 | + seenNames.add("start"); |
| 48 | + seenNames.add("stop"); |
| 49 | + |
| 50 | + for (final Method method : Selenium.class.getMethods()) { |
| 51 | + if (!seenNames.add(method.getName())) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + if (method.getParameterCount() > 3) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + SeleneseCommand underlyingCommand = (driver, selenium, locator, value) -> { |
| 60 | + try { |
| 61 | + switch (method.getParameterCount()) { |
| 62 | + case 0: |
| 63 | + return method.invoke(selenium); |
| 64 | + |
| 65 | + case 1: |
| 66 | + return method.invoke(selenium, locator); |
| 67 | + |
| 68 | + case 2: |
| 69 | + return method.invoke(selenium, locator, value); |
| 70 | + |
| 71 | + default: |
| 72 | + throw new RuntimeException("Exceptionally unlikely to get here"); |
| 73 | + } |
| 74 | + } catch (ReflectiveOperationException e) { |
| 75 | + throw new RuntimeException(e); |
| 76 | + } |
| 77 | + }; |
| 78 | + commands.put(method.getName(), underlyingCommand); |
| 79 | + |
| 80 | + // Methods of the form getFoo(target) result in commands: |
| 81 | + // getFoo, assertFoo, verifyFoo, assertNotFoo, verifyNotFoo |
| 82 | + // storeFoo, waitForFoo, and waitForNotFoo. |
| 83 | + final String shortName; |
| 84 | + if (method.getName().startsWith("get")) { |
| 85 | + shortName = method.getName().substring("get".length()); |
| 86 | + } else if (method.getName().startsWith("is")) { |
| 87 | + shortName = method.getName().substring("is".length()); |
| 88 | + } else { |
| 89 | + shortName = null; |
| 90 | + } |
| 91 | + |
| 92 | + if (shortName != null) { |
| 93 | + SeleneseCommand performComparison = (driver, selenium, locator, value) -> { |
| 94 | + Object result = underlyingCommand.execute(driver, selenium, locator, value); |
| 95 | + if ("is".equals(shortName)) { |
| 96 | + return (Boolean) result; |
| 97 | + } |
| 98 | + |
| 99 | + String comparisonValue; |
| 100 | + switch (method.getParameterCount()) { |
| 101 | + case 0: |
| 102 | + comparisonValue = locator; |
| 103 | + break; |
| 104 | + |
| 105 | + case 1: |
| 106 | + comparisonValue = value; |
| 107 | + break; |
| 108 | + |
| 109 | + default: |
| 110 | + throw new RuntimeException("Unsure how to process this assert: " + method.getName()); |
| 111 | + } |
| 112 | + return Objects.equals(comparisonValue, String.valueOf(result)); |
| 113 | + }; |
| 114 | + |
| 115 | + commands.put("assert" + shortName, (driver, selenium, locator, value) -> { |
| 116 | + boolean result = (Boolean) performComparison.execute(driver, selenium, locator, value); |
| 117 | + if (!result) { |
| 118 | + throw new SeleniumException("Assertion failed"); |
| 119 | + } |
| 120 | + return null; |
| 121 | + }); |
| 122 | + |
| 123 | + commands.put("assertNot" + shortName, (driver, selenium, locator, value) -> { |
| 124 | + boolean result = (Boolean) performComparison.execute(driver, selenium, locator, value); |
| 125 | + if (result) { |
| 126 | + throw new SeleniumException("Negative assertion failed"); |
| 127 | + } |
| 128 | + return null; |
| 129 | + }); |
| 130 | + |
| 131 | + commands.put("verify" + shortName, (driver, selenium, locator, value) -> { |
| 132 | + boolean result = (Boolean) performComparison.execute(driver, selenium, locator, value); |
| 133 | + if (!result) { |
| 134 | + System.out.println("Verification failed"); |
| 135 | + } |
| 136 | + return null; |
| 137 | + }); |
| 138 | + |
| 139 | + commands.put("verifyNot" + shortName, (driver, selenium, locator, value) -> { |
| 140 | + boolean result = (Boolean) performComparison.execute(driver, selenium, locator, value); |
| 141 | + if (result) { |
| 142 | + System.out.println("Negative verification failed"); |
| 143 | + } |
| 144 | + return null; |
| 145 | + }); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + commands.put("pause", (driver, selenium, locator, value) -> { |
| 150 | + try { |
| 151 | + long timeout = Long.parseLong(locator); |
| 152 | + Thread.sleep(timeout); |
| 153 | + return null; |
| 154 | + } catch (NumberFormatException e) { |
| 155 | + throw new SeleniumException("Unable to parse timeout: " + locator); |
| 156 | + } catch (InterruptedException e) { |
| 157 | + System.exit(255); |
| 158 | + throw new RuntimeException("We never get this far"); |
| 159 | + } |
| 160 | + }); |
| 161 | + |
| 162 | + return commands.build(); |
| 163 | + } |
| 164 | + |
| 165 | + private interface SeleneseCommand { |
| 166 | + Object execute(WebDriver driver, Selenium selenium, String locator, String value); |
| 167 | + } |
| 168 | +} |
0 commit comments