How to use getShadowRoot method of org.openqa.selenium.Interface WebElement class

Best Selenium code snippet using org.openqa.selenium.Interface WebElement.getShadowRoot

Source:Driver.java Github

copy

Full Screen

...916 }917 static DriverFunction<WebElement> getElement(LazyLocator locator) {918 return ifDriver("getElement", FrameworkCoreFormatter.isInvalidLazyLocatorMessage(locator, SeleniumUtilities::getLocator), getElementFromSingle(getSingleElementList(locator)), SeleniumDataConstants.NULL_ELEMENT);919 }920 private static Data<WebElement> getShadowRootElementCore(Data<WebElement> data) {921 final var exception = data.exception;922 final var status = isNotNullWebElement(data);923 final var messageData = new MethodMessageData("getShadowRootElementCore", SeleniumFormatter.getShadowRootElementMessage(data.message.getMessage(), status));924 return CoreUtilities.isNonException(exception) ? (925 DataFactoryFunctions.getWithMethodMessage(data.object, status, messageData)926 ) : DataFactoryFunctions.getWithMethodMessage(SeleniumCoreConstants.STOCK_ELEMENT, false, messageData, exception);927 }928 private static DriverFunction<WebElement> getShadowRootElement(Data<WebElement> data) {929 return ifDriver(930 "getShadowRootElement",931 isInvalidOrFalseMessage(data),932 validChain(Execute.getShadowRootElement(data), Driver::getShadowRootElementCore, SeleniumDataConstants.NULL_ELEMENT),933 SeleniumDataConstants.NULL_ELEMENT934 );935 }936 static DriverFunction<WebElement> getShadowRootElement(DriverFunction<WebElement> getter) {937 return ifDriver("getShadowRootElement", CoreFormatter.isNullMessageWithName(getter, "Getter"), Execute.getShadowRoot(getter), SeleniumDataConstants.NULL_ELEMENT);938 }939 static DriverFunction<WebElement> getShadowRootElement(LazyElement element) {940 return getShadowRootElement(element.get());941 }942 static DriverFunction<WebElement> getShadowRootElement(By locator, SingleGetter getter) {943 return ifDriver("getShadowRootElement", areNotNull(locator, getter), getShadowRootElement(LocatorRepository.getIfContains(locator, getter).object), SeleniumDataConstants.NULL_ELEMENT);944 }945 static DriverFunction<WebElement> getShadowRootElement(By locator) {946 return getShadowRootElement(locator, SingleGetter.DEFAULT);947 }948 static DriverFunction<WebElement> getRootElementByInvokedElement(Data<WebElement> data, By locator) {949 final var nameof = "getRootElementByInvokedElement";950 return ifDriver(951 nameof,952 isNotNull(locator) && isValidNonFalse(data),953 getShadowRootElement(invokeGetElement(locator).apply(getSearchContextOf("Element data", data))),954 replaceMessage(SeleniumDataConstants.NULL_ELEMENT, nameof, "Parameters were wrong: locator or data.")955 );956 }957 static DriverFunction<WebElement> getRootElementByInvokedElement(LazyElement element, By locator) {958 final var nameof = "getRootElementByInvokedElement";959 return ifDriver(960 nameof,961 isNotNullLazyElement(element) && isNotNull(locator),962 getRootElementByInvokedElement(element, SeleniumLazyLocatorFactory.get(locator)),963 replaceMessage(SeleniumDataConstants.NULL_ELEMENT, nameof, "Parameters were wrong: locator or data.")964 );965 }966 static DriverFunction<WebElement> getRootElementByInvokedElement(Data<WebElement> data, LazyLocator locator) {967 final var nameof = "getRootElementByInvokedElement";968 return ifDriver(969 nameof,970 isNotNullWebElement(data) && isNotNullLazyData(locator),971 getShadowRootElement(invokeGetElement(getLocator(locator).object).apply(getSearchContextOf("Element data", data))),972 replaceMessage(SeleniumDataConstants.NULL_ELEMENT, nameof, "Parameters were wrong: locator or data.")973 );974 }975 static DriverFunction<WebElement> getRootElementByInvokedElement(LazyElement element, LazyLocator locator) {976 return ifDriver(977 "getRootElementByInvokedElement",978 isNotNullLazyElement(element) && isNotNullLazyData(locator),979 driver -> getShadowRootElement(invokeGetElement(getLocator(locator).object).apply(getSearchContextOf("Element data", element.get().apply(driver)))).apply(driver),980 SeleniumDataConstants.NULL_ELEMENT//, nameof, "Parameters were wrong: locator or data.")981 );982 }983 static DriverFunction<WebElement> getShadowRootElement(Data<WebElement> data, LazyLocatorList locators) {984 return ifDriver(985 "getShadowRootElement",986 isNotNullWebElement(data) && isNotNull(locators) && locators.isNotNullAndNonEmpty(),987 driver -> {988 if (locators.isSingle()) {989 return getRootElementByInvokedElement(data, locators.first()).apply(driver);990 }991 var current = data;992 var message = "";993 for (var shadowLocator : locators) {994 if (isNull(shadowLocator)) {995 break;996 }997 current = getRootElementByInvokedElement(current, shadowLocator).apply(driver);998 if (isNullWebElement(current)) {999 break;1000 }1001 message += current.message;1002 }1003 return replaceMessage(current, message);1004 },1005 SeleniumDataConstants.NULL_ELEMENT1006 );1007 }1008 static Function<Data<WebElement>, DriverFunction<WebElement>> getShadowRootElementFunction(LazyLocatorList locators) {1009 return isNotNull(locators) && locators.isNotNullAndNonEmpty() ? (1010 data -> getShadowRootElement(data, locators)1011 ) : data -> DriverFunctionFactory.getWithMessage(SeleniumCoreConstants.STOCK_ELEMENT, false, SeleniumFormatterConstants.LOCATOR_WAS_NULL);1012 }1013 static DriverFunction<WebElement> getShadowRootElement(LazyLocatorList locators) {1014 final var nameof = "getShadowRootElement";1015 return ifDriver(1016 nameof,1017 isNotNull(locators) && locators.isNotNullAndNonEmpty() && isNotNullLazyData(locators.first()),1018 driver -> {1019 final var currentBy = locators.first();1020 final var current = getShadowRootElement(getLocator(currentBy).object).apply(driver);1021 if (isNullWebElement(current)) {1022 return prependMessage(SeleniumDataConstants.NULL_ELEMENT, "Current " + CoreFormatterConstants.WAS_NULL + current.message.toString());1023 }1024 return locators.isMany() ? getShadowRootElement(current, locators.tail()).apply(driver) : current;1025 },1026 prependMessage(SeleniumDataConstants.NULL_ELEMENT, nameof, "Shadow locators list" + CoreFormatterConstants.WAS_NULL)1027 );1028 }1029 static DriverFunction<WebElement> getShadowRootElement(DriverFunction<WebElement> data, LazyLocatorList locators) {1030 return ifDriver(1031 "getShadowRootElement",1032 areNotNull(data, locators) && locators.isNotNullAndNonEmpty(),1033 driver -> {1034 var current = data.apply(driver);1035 return isNotNullWebElement(current) ? getShadowRootElement(current, locators).apply(driver) : SeleniumDataConstants.NULL_ELEMENT;1036 },1037 SeleniumDataConstants.NULL_ELEMENT1038 );1039 }1040 static DriverFunction<WebElement> getShadowRootElement(LazyElement element, LazyLocatorList locators) {1041 return ifDriver("getShadowRootElement", areNotNull(element, locators) && locators.isNotNullAndNonEmpty(), getShadowRootElement(element.get(), locators), SeleniumDataConstants.NULL_ELEMENT);1042 }1043 static Function<Data<SearchContext>, Data<WebElement>> getNestedElement(By locator) {1044 final var nameof = "getNestedElement: ";1045 return isNotNull(locator) ? (context -> {1046 if (isNull(context)) {1047 return replaceMessage(SeleniumDataConstants.NULL_ELEMENT, nameof, CoreFormatterConstants.PASSED_DATA_WAS_NULL);1048 }1049 final var nestedElement = getNestedElementsAmount(locator, 1).apply(context);1050 if (isInvalidOrFalse(nestedElement)) {1051 return replaceMessage(SeleniumDataConstants.NULL_ELEMENT, nameof, "Nested Element" + CoreFormatterConstants.WAS_NULL);1052 }1053 final var object = nestedElement.object;1054 return isNotNull(object) && object.isNotNullAndNonEmpty() ? (1055 getElementFromSingle(nestedElement)1056 ) : replaceMessage(SeleniumDataConstants.NULL_ELEMENT, nameof, "Nested Element" + CoreFormatterConstants.WAS_NULL + nestedElement.message);1057 }) : context -> replaceMessage(SeleniumDataConstants.NULL_ELEMENT, nameof, SeleniumFormatterConstants.LOCATOR_WAS_NULL);1058 }1059 static Function<Data<SearchContext>, Data<WebElementList>> getNestedElements(By locator) {1060 return context -> {1061 final var nameof = "getNestedElements";1062 final var message = SeleniumFormatter.getNestedElementsErrorMessage(locator, context);1063 return isBlank(message) ? (1064 replaceName(getElementsCore(getSearchContextOf("Search Context", context), SeleniumLazyLocatorFactory.get(locator)), nameof)1065 ) : replaceMessage(SeleniumDataConstants.NULL_LIST, nameof, message);1066 };1067 }1068 static Function<Data<SearchContext>, Data<WebElement>> getNestedElement(LazyLocator locator) {1069 final var nameof = "getNestedElement";1070 return isNotNullLazyData(locator) ? getNestedElement(getLocator(locator).object) : data -> replaceMessage(SeleniumDataConstants.NULL_ELEMENT, nameof, SeleniumFormatterConstants.LOCATOR_WAS_NULL);1071 }1072 static Function<Data<SearchContext>, Data<WebElementList>> getNestedElements(LazyLocator locator) {1073 final var nameof = "getNestedElements";1074 if (isNullLazyLocator(locator)) {1075 return context -> replaceMessage(SeleniumDataConstants.NULL_LIST, nameof + "Lazy locator" + CoreFormatterConstants.WAS_NULL);1076 }1077 final var locatorData = getLocator(locator);1078 return isValidNonFalse(locatorData) ? getNestedElements(locatorData.object) : context -> replaceMessage(SeleniumDataConstants.NULL_LIST, nameof + "Locator data" + CoreFormatterConstants.WAS_NULL);1079 }1080 static Function<Data<SearchContext>, Data<WebElementList>> getNestedElementsAmount(By locator, int count) {1081 final var nameof = "getNestedElementsAmount";1082 return isNotNull(locator) && BasicPredicateFunctions.isNonNegative(count) ? (context -> {1083 if (isNull(context)) {1084 return replaceMessage(SeleniumDataConstants.NULL_LIST, CoreFormatterConstants.PASSED_DATA_WAS_NULL);1085 }1086 final var result = getNestedElements(locator).apply(context);1087 return getElementsAmountCore(locator, count).apply(result);1088 }) : context -> replaceMessage(SeleniumDataConstants.NULL_LIST, nameof + "Locator was null, or count was wrong. Locator: " + locator + ", count: " + count);1089 }1090 static Function<Data<SearchContext>, Data<Integer>> getCountOfNestedElements(By locator) {1091 return isNotNull(locator) ? (1092 context -> isNotNull(context) ? getCountOfElements(getNestedElements(locator).apply(context)) : CoreDataConstants.NO_ELEMENTS_FOUND_DATA_FALSE_OR_NULL1093 ) : context -> SeleniumDataConstants.NO_ELEMENTS_FOUND;1094 }1095 private static <T> DriverFunction<T> getShadowNested(Function<LazyLocator, Function<Data<SearchContext>, Data<T>>> getter, LazyLocatorList locators, LazyLocator locator, T defaultValue) {1096 final var nameof = "getShadowNested";1097 return ifDriver(1098 nameof,1099 areNotNull(getter, locators, defaultValue) && locators.isNotNullAndNonEmpty() && isNotNullLazyData(locator),1100 driver -> isValidNonFalse(switchToDefaultContent().apply(driver)) ? (1101 getter.apply(locator).apply(getSearchContext(getShadowRootElement(locators).apply(driver).object))1102 ) : DataFactoryFunctions.getInvalidWithNameAndMessage(defaultValue, nameof, "Couldn't switch to default content" + CoreFormatterConstants.END_LINE),1103 DataFactoryFunctions.getWithMessage(defaultValue, false, "There were parameter issues" + CoreFormatterConstants.END_LINE)1104 );1105 }1106 static DriverFunction<WebElement> getShadowNestedElement(LazyLocatorList locators, LazyLocator elementLocator) {1107 return getShadowNested(Driver::getNestedElement, locators, elementLocator, SeleniumCoreConstants.STOCK_ELEMENT);1108 }1109 static DriverFunction<WebElementList> getShadowNestedElements(LazyLocatorList locators, LazyLocator elementLocator) {1110 return getShadowNested(Driver::getNestedElements, locators, elementLocator, SeleniumCoreConstants.NULL_ELEMENT_LIST);1111 }1112 static DriverFunction<WebElement> getShadowNestedElement(LazyLocatorList locators, By locator) {1113 return getShadowNestedElement(locators, SeleniumLazyLocatorFactory.get(locator));1114 }1115 static DriverFunction<WebElementList> getShadowNestedElements(LazyLocatorList locators, By locator) {1116 return getShadowNestedElements(locators, SeleniumLazyLocatorFactory.get(locator));1117 }1118 static <T> Data<Boolean> switchTo(1119 TargetLocator locator,1120 Function<TargetLocator, T> operation,1121 BiFunction<Boolean, SwitchResultMessageData<Void>, String> formatter,1122 SwitchResultMessageData<Void> messageData1123 ) {1124 final var nameof = "switchTo";1125 if (isNull(locator)) {1126 DataFactoryFunctions.getBoolean(false, nameof, formatter.apply(false, messageData));1127 }1128 var exception = CoreConstants.EXCEPTION;1129 try {1130 operation.apply(locator);1131 } catch (NoSuchFrameException ex) {1132 exception = ex;1133 }1134 var status = CoreUtilities.isNonException(exception);1135 return DataFactoryFunctions.getBoolean(status, nameof, formatter.apply(status, messageData), exception);1136 }1137 static <T, U> Data<Boolean> switchTo(1138 T target,1139 TargetLocator locator,1140 boolean guardCondition,1141 BiFunction<TargetLocator, T, U> operation,1142 BiFunction<Boolean, SwitchResultMessageData<T>, String> formatter,1143 SwitchResultMessageData<T> messageData1144 ) {1145 final var nameof = "switchTo";1146 if (areAnyNull(target, locator) || !guardCondition) {1147 DataFactoryFunctions.getInvalidBooleanWithNameAndMessage(nameof, formatter.apply(false, messageData));1148 }1149 var exception = CoreConstants.EXCEPTION;1150 try {1151 operation.apply(locator, target);1152 } catch (NoSuchFrameException ex) {1153 exception = ex;1154 }1155 var status = CoreUtilities.isNonException(exception);1156 return DataFactoryFunctions.getBoolean(status, nameof, formatter.apply(status, messageData), exception);1157 }1158 static <T, U> Data<Boolean> switchTo(T target, TargetLocator locator, BiFunction<TargetLocator, T, U> operation, String type, String nameof) {1159 return switchTo(target, locator, true, operation, SeleniumFormatter::getSwitchToMessage, new SwitchResultMessageData<T>(target, type, nameof));1160 }1161 static <T, U> Function<TargetLocator, Data<Boolean>> switchTo(T target, BiFunction<TargetLocator, T, U> operation, String type, String nameof) {1162 return locator -> switchTo(target, locator, operation, type, nameof);1163 }1164 static Function<TargetLocator, Data<Boolean>> switchToFrame(Data<WebElement> data) {1165 final var nameof = "switchToFrame(Data<WebElement> data): ";1166 return target -> ifData(1167 nameof,1168 NullableFunctions::isNotNull,1169 target,1170 switchTo(data.object, TargetLocator::frame, "frame", nameof),1171 DataFactoryFunctions.getBoolean(false, nameof, "Couldn't attempt, data was null or false" + CoreFormatterConstants.END_LINE)1172 );1173 }1174 static Function<TargetLocator, Data<Boolean>> switchToFrameSingleList(Data<WebElementList> data) {1175 final var nameof = "switchToFrame(Data<WebElement> data): ";1176 if (isInvalidOrFalse(data)) {1177 return SeleniumDataConstants.SWITCH_TO_NEGATIVE;1178 }1179 final var list = data.object;1180 final var element = DataFactoryFunctions.getWithNameAndMessage(list.first(), list.isSingle(), nameof, data.message.message);1181 return isValidNonFalse(data) ? switchToFrame(element) : SeleniumDataConstants.SWITCH_TO_NEGATIVE;1182 }1183 static DriverFunction<Boolean> switchToFrame(DriverFunction<WebElement> data) {1184 final var nameof = "switchToFrame";1185 return ifDriver(1186 nameof,1187 isNotNull(data),1188 driver -> switchToFrame(data.apply(driver)).apply(driver.switchTo()),1189 DataFactoryFunctions.getBoolean(false, nameof, "Data parameter " + CoreFormatterConstants.WAS_NULL)1190 );1191 }1192 static DriverFunction<Boolean> switchToFrame(By locator, Function<By, DriverFunction<WebElement>> getter) {1193 final var nameof = "switchToFrame";1194 return ifDriver(1195 nameof,1196 areNotNull(locator, getter),1197 switchToFrame(getter.apply(locator)),1198 replaceMessage(CoreDataConstants.NULL_BOOLEAN, nameof, "Couldn't attempt switchToFrame.")1199 );1200 }1201 static DriverFunction<Boolean> switchToFrame(Data<By> locator, Function<By, DriverFunction<WebElement>> getter) {1202 final var nameof = "switchToFrame";1203 return ifDriver(1204 nameof,1205 isValidNonFalse(locator) && isNotNull(getter),1206 switchToFrame(getter.apply(locator.object)),1207 replaceMessage(CoreDataConstants.NULL_BOOLEAN, nameof, "Couldn't attempt switchToFrame.")1208 );1209 }1210 static DriverFunction<Boolean> switchToFrame(By locator) {1211 return switchToFrame(locator, Driver::getElement);1212 }1213 static DriverFunction<Boolean> switchToFrame(LazyLocator locator) {1214 return switchToFrame(getLocator(locator), Driver::getElement);1215 }1216 static DriverFunction<Boolean> switchToFrame(LazyLocatorList locator) {1217 final var nameof = "switchToFrame";1218 return ifDriver(1219 nameof,1220 isNotNull(locator) && locator.isSingle(),1221 switchToFrame(getLocator(locator.first()), Driver::getElement),1222 replaceMessage(CoreDataConstants.NULL_BOOLEAN, nameof, "Couldn't attempt switchToFrame.")1223 );1224 }1225 static DriverFunction<Boolean> switchToFrameFromSingle(LazyLocatorList locators) {1226 final var nameof = "switchToFrameFromSingle";1227 return ifDriver(1228 nameof,1229 isNotNull(locators) && locators.isSingle(),1230 switchToFrame(locators.first()),1231 replaceMessage(CoreDataConstants.NULL_BOOLEAN, nameof, "Couldn't attempt switchToFrame. Non-singular list used in function")1232 );1233 }1234 static DriverFunction<Boolean> switchToFrame(int target) {1235 return driver -> switchTo(1236 target,1237 driver.switchTo(),1238 BasicPredicateFunctions.isNonNegative(target),1239 TargetLocator::frame,1240 SeleniumFormatter::getSwitchToMessage,1241 new SwitchResultMessageData<Integer>(target, "frame", "switchToFrame(int frameLocator): ")1242 );1243 }1244 static DriverFunction<Boolean> switchToWindow(String target) {1245 return driver -> switchTo(1246 target,1247 driver.switchTo(),1248 isNotBlank(target),1249 TargetLocator::frame,1250 SeleniumFormatter::getSwitchToMessage,1251 new SwitchResultMessageData<String>(target, "window", "switchToWindow(String target): ")1252 );1253 }1254 static DriverFunction<Boolean> switchToParentFrame() {1255 return driver -> switchTo(driver.switchTo(), TargetLocator::parentFrame, SeleniumFormatter::getSwitchToMessage, new SwitchResultMessageData<Void>(null, "parent frame", "switchToParentFrame: "));1256 }1257 static DriverFunction<Boolean> switchToAlert() {1258 return driver -> switchTo(driver.switchTo(), TargetLocator::alert, SeleniumFormatter::getSwitchToMessage, new SwitchResultMessageData<Void>(null, "alert.", "switchToAlert: "));1259 }1260 static DriverFunction<Boolean> switchToDefaultContent() {1261 return driver -> switchTo(driver.switchTo(), TargetLocator::defaultContent, SeleniumFormatter::getSwitchToMessage, new SwitchResultMessageData<Void>(null, "default content.", "switchToDefaultContent: "));1262 }1263 static <T> DriverFunction<T> switchToDefaultContentWith(DriverFunction<T> action) {1264 return SeleniumExecutor.execute(switchToDefaultContent(), action);1265 }1266 static DriverFunction<Boolean> switchToNestedFrame(LazyLocatorList locators) {1267 return ifDriver(1268 "switchToNestedFrame",1269 isNotNull(locators) && locators.isNotNullAndNonEmpty(),1270 driver -> {1271 if (isInvalidOrFalse(switchToDefaultContent().apply(driver))) {1272 return replaceMessage(CoreDataConstants.NULL_BOOLEAN, "Couldn't switch to default content.");1273 }1274 final var length = locators.size();1275 var message = "";1276 var index = 0;1277 LazyLocator locator;1278 Data<?> data;1279 for(; index < length; ++index) {1280 locator = locators.get(index);1281 message += index + ".: ";1282 if (isNull(locator)) {1283 message += (SeleniumFormatterConstants.LOCATOR_WAS_NULL);1284 break;1285 }1286 data = switchToFrame(Driver.getElement(locator)).apply(driver);1287 message += data.message;1288 if (isValidNonFalse(data)) {1289 break;1290 }1291 }1292 return DataFactoryFunctions.getBoolean(index == length, message);1293 },1294 replaceMessage(CoreDataConstants.NULL_BOOLEAN, "Locators were empty.")1295 );1296 }1297 static DriverFunction<WebElementList> getFrameNestedElements(LazyLocatorList locators) {1298 return ifDriver(1299 "getFrameNestedElements",1300 isNotNull(locators) && locators.isNotNullAndNonEmpty(),1301 driver -> {1302 if (locators.isSingle()) {1303 return Driver.getElements(locators.first()).apply(driver);1304 }1305 final var data = switchToNestedFrame(locators.initials()).apply(driver);1306 return isValidNonFalse(data) ? Driver.getElements(locators.last()).apply(driver) : SeleniumDataConstants.NULL_LIST;1307 },1308 SeleniumDataConstants.NULL_LIST1309 );1310 }1311 static DriverFunction<WebElement> getFrameNestedElement(LazyLocatorList locators, Function<By, DriverFunction<WebElement>> getter) {1312 return ifDriver(1313 "getFrameNestedElement",1314 areNotNull(getter, locators) && locators.isMany() && !locators.hasMoreThan(2),1315 driver -> {1316 final var frameData = locators.first();1317 if (isNullLazyLocator(frameData)) {1318 return SeleniumDataConstants.NULL_ELEMENT;1319 }1320 final var frameLocator = getLocator(frameData).object;1321 if (isInvalidOrFalse(getter.apply(frameLocator).apply(driver))) {1322 return SeleniumDataConstants.NULL_ELEMENT;1323 }1324 final var elementData = locators.last();1325 if (isNullLazyLocator(elementData)) {1326 return SeleniumDataConstants.NULL_ELEMENT;1327 }1328 final var elementLocator = getLocator(elementData).object;1329 if (isInvalidOrFalse(getter.apply(frameLocator).apply(driver))) {1330 return SeleniumDataConstants.NULL_ELEMENT;1331 }1332 final var data = switchToFrame(getter.apply(frameLocator).apply(driver)).apply(driver.switchTo());1333 return isValidNonFalse(data) ? getter.apply(elementLocator).apply(driver) : SeleniumDataConstants.NULL_ELEMENT;1334 },1335 SeleniumDataConstants.NULL_ELEMENT1336 );1337 }1338 static <T, U> DriverFunction<U> getWithLocator(GetWithDriverData<DecoratedList<T>, T, T, U> data) {1339 return DriverFunctionFactory.getFunction(DataExecutionFunctions.ifDependency("getWithLocator", areNotNull(data.locatorGetter, data.getter), data.getter.apply(data.locatorGetter.apply(data.locators)), data.guardData));1340 }1341 static <T> DriverFunction<T> getWithLazyLocator(GetWithDriverData<LazyLocatorList, LazyLocator, By, T> data) {1342 return DriverFunctionFactory.getFunction(DataExecutionFunctions.ifDependency("getWithLazyLocator", areNotNull(data.locatorGetter, data.getter), data.getter.apply(getLocator(data.locatorGetter.apply(data.locators)).object), data.guardData));1343 }1344 static <WE, BY, BYY extends By, W extends DecoratedList<BY>> DriverFunction<WE> getFromSingle(1345 Function<GetWithDriverData<W, BY, BYY, WE>, DriverFunction<WE>> getter,1346 GetWithDriverData<W, BY, BYY, WE> data,1347 String nameof1348 ) {1349 return ifDriver(nameof, data.locators.isSingle(), getter.apply(data), data.guardData);1350 }1351 static DriverFunction<WebElement> getShadowNestedElement(LazyLocatorList locators) {1352 if (isNull(locators) || !locators.hasAtleast(2)) {1353 return DriverFunctionFactory.get(prependMessage(SeleniumDataConstants.NULL_ELEMENT, "Lazy Locator list doesn't have enough items" + CoreFormatterConstants.END_LINE));1354 }1355 final var start = locators.first();1356 final var tail = locators.tail();1357 return isNotNullLazyData(start) && isNotNull(tail) ? (1358 getShadowNestedElement(tail, start)1359 ) : DriverFunctionFactory.get(prependMessage(SeleniumDataConstants.NULL_ELEMENT, "Lazy locator item issues" + CoreFormatterConstants.END_LINE));1360 }1361 static DriverFunction<WebElement> getElementFromSingle(LazyLocatorList locator) {1362 return getFromSingle(1363 Driver::getWithLazyLocator,1364 new GetWithDriverData<LazyLocatorList, LazyLocator, By, WebElement>(locator, LazyLocatorList::first, Driver::getElement, SeleniumDataConstants.NULL_ELEMENT),1365 "getElementFromSingle"1366 );1367 }1368 static DriverFunction<WebElement> getRootElementFromSingle(LazyLocatorList locator) {1369 return getFromSingle(1370 Driver::getWithLazyLocator,1371 new GetWithDriverData<LazyLocatorList, LazyLocator, By, WebElement>(locator, LazyLocatorList::first, Driver::getShadowRootElement, SeleniumDataConstants.NULL_ELEMENT),1372 "getRootElementFromSingle"1373 );1374 }1375 static DriverFunction<WebElement> getNestedElement(LazyLocatorList locators) {1376 final var nameof = "getNestedElement";1377 return ifDriver(1378 nameof,1379 isNotNull(locators) && locators.hasAtleast(2) && isNotNullLazyData(locators.first()),1380 driver -> {1381 if (isInvalidOrFalse(switchToDefaultContent().apply(driver))) {1382 return DataFactoryFunctions.getWithNameAndMessage(SeleniumCoreConstants.STOCK_ELEMENT, false, nameof, "Driver was null or couldn't switch to default content" + CoreFormatterConstants.END_LINE);1383 }1384 var locator = locators.first();1385 var data = Driver.getElement(locator).apply(driver);...

Full Screen

Full Screen

Source:WebDriverTestCase.java Github

copy

Full Screen

...821 * 822 * @param element the WebElement to retrieve the shadowRoot property of.823 * @return the shadowRoot DOM subtree of a given WebElement. 824 */825 public WebElement getShadowRoot(WebElement element) {826 return (WebElement) ((JavascriptExecutor)getDriver()).executeScript("return arguments[0].shadowRoot", element);827 }828 /**829 * Returns an element inside of a Shadow DOM. The list of passed in selectors will be iterated over and at each830 * element found, the shadowRoot property will be expanded before doing the next query.831 * 832 * @param selectors an array of By selectors where each selector besides the final one is a shadow boundary.833 * @return an element inside a Shadow DOM or null if none is found.834 */835 public WebElement findElementInShadowDom(By[] selectors) {836 WebElement curr = null;837 for (int i = 0; i < selectors.length; i++) {838 if (curr == null) {839 curr = findDomElement(selectors[i]);840 } else {841 curr = curr.findElement(selectors[i]);842 }843 if (i != selectors.length-1) {844 curr = getShadowRoot(curr);845 }846 }847 return curr;848 }849}...

Full Screen

Full Screen

Source:Execute.java Github

copy

Full Screen

...156 },157 CoreDataConstants.NULL_OBJECT158 );159 }160 private static DriverFunction<WebElement> getShadowRootCore(Data<WebElement> data) {161 return ifDriver(162 "getShadowRootCore",163 SeleniumUtilities.isNotNullWebElement(data),164 driver -> {165 final var parameter = handleDataParameterDefault(data);166 if(DataPredicates.isInvalidOrFalse(parameter)) {167 return DataFactoryFunctions.replaceMessage(SeleniumDataConstants.NULL_ELEMENT, DataFunctions.getFormattedMessage(parameter));168 }169 final var result = Driver.executeSingleParameter(ShadowRoot.GET_SHADOW_ROOT, parameter.object).apply(driver);170 return DataPredicates.isValidNonFalse(result) ? (171 getWith((WebElement)result.object, result.status, result.message)172 ) : DataFactoryFunctions.replaceMessage(SeleniumDataConstants.NULL_ELEMENT, DataFunctions.getFormattedMessage(data));173 },174 SeleniumDataConstants.NULL_ELEMENT175 );176 }177 static DriverFunction<WebElement> getShadowRootElement(Data<WebElement> data) {178 return getShadowRootCore(data);179 }180 static DriverFunction<WebElement> getShadowRoot(DriverFunction<WebElement> getter) {181 return ExecutionCore.ifDriverFunction("getShadowRoot", NullableFunctions::isNotNull, getter, Execute::getShadowRootCore, SeleniumDataConstants.NULL_ELEMENT);182 }183 static DriverFunction<WebElement> getShadowRoot(LazyElement element) {184 return SeleniumUtilities.isNotNullLazyElement(element) ? (185 getShadowRoot(element.get())186 ) : DriverFunctionFactory.get(DataFactoryFunctions.replaceMessage(SeleniumDataConstants.NULL_ELEMENT, "getShadowRoot", "LazyElement element" + CoreFormatterConstants.WAS_NULL));187 }188 static DriverFunction<WebElement> getShadowRoot(Data<LazyElement> data) {189 return ExecutionCore.ifDriver("getShadowRoot", DataPredicates.isValidNonFalse(data), getShadowRoot(data.object), SeleniumDataConstants.NULL_ELEMENT);190 }191 static DriverFunction<WebElement> getShadowRoot(By locator, SingleGetter getter) {192 return getShadowRoot(LocatorRepository.getIfContains(locator, getter));193 }194 static DriverFunction<WebElement> getShadowRoot(By locator) {195 return getShadowRoot(locator, SingleGetter.DEFAULT);196 }197 static DriverFunction<Boolean> readyState() {198 final var negative = CoreDataConstants.NULL_BOOLEAN;199 return ExecutionCore.ifDriver(200 "readyState",201 driver -> {202 final var result = Driver.execute(ReadyState.script).apply(driver);203 return DataPredicates.isValidNonFalse(result) ? DataFactoryFunctions.replaceObject(result, Boolean.valueOf(result.object.toString())) : negative;204 },205 negative206 );207 }208 static DriverFunction<String> setAttribute(Data<WebElement> element, String attribute, String value) {209 final var nameof = "setAttribute";...

Full Screen

Full Screen

Source:WebElement.java Github

copy

Full Screen

...253 /**254 * @return A representation of an element's shadow root for accessing the shadow DOM of a web component.255 * @throws NoSuchShadowRootException If no shadow root is found256 */257 default SearchContext getShadowRoot() {258 throw new UnsupportedOperationException("getShadowRoot");259 }260 /**261 * Is this element displayed or not? This method avoids the problem of having to parse an262 * element's "style" attribute.263 *264 * @return Whether or not the element is displayed265 */266 boolean isDisplayed();267 /**268 * Where on the page is the top left-hand corner of the rendered element?269 * <p>270 * See <a href="https://w3c.github.io/webdriver/#get-element-rect">W3C WebDriver specification</a>271 * for more details.272 *...

Full Screen

Full Screen

Source:GrapheneElementImpl.java Github

copy

Full Screen

...259 public String getDomProperty(String name) {260 return element.getDomProperty(name);261 }262 @Override263 public SearchContext getShadowRoot() {264 return element.getShadowRoot();265 }266 @Override267 public int hashCode() {268 if (element == null) {269 // shouldn't ever happen270 return super.hashCode();271 }272 // see #equals for explanation273 return element.hashCode();274 }275 @Override276 public boolean equals(Object obj) {277 if (element == null) {278 // shouldn't ever happen...

Full Screen

Full Screen

getShadowRoot

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.support.ui.ExpectedConditions;6import org.openqa.selenium.support.ui.WebDriverWait;7public class ShadowDom {8public static void main(String[] args) {9 System.setProperty("webdriver.chrome.driver", "C:\\Users\\dell\\Downloads\\chromedriver_win32\\chromedriver.exe");10 WebDriver driver = new ChromeDriver();11 driver.manage().window().maximize();12 WebElement shadowRoot = driver.findElement(By.tagName("body")).findElement(By.tagName("app-root")).findElement(By.tagName("app-checkbox")).findElement(By.tagName("div")).findElement(By.tagName("div")).findElement(By.tagName("div"));13 WebElement checkbox = shadowRoot.findElement(By.cssSelector("input[type='checkbox']"));14 WebDriverWait wait = new WebDriverWait(driver, 10);15 wait.until(ExpectedConditions.elementToBeClickable(checkbox));16 if (!checkbox.isSelected()) {17 checkbox.click();18 }19 driver.quit();20}21}

Full Screen

Full Screen

getShadowRoot

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5public class ShadowDomExample {6 public static void main(String[] args) {7 System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\Downloads\\chromedriver.exe");8 WebDriver driver = new ChromeDriver();9 WebElement root1 = driver.findElement(By.cssSelector("custom-checkbox"));10 WebElement shadowRoot1 = expandRootElement(root1);11 WebElement checkbox = shadowRoot1.findElement(By.cssSelector("input"));12 checkbox.click();13 }14 public static WebElement expandRootElement(WebElement element) {15 WebElement ele = (WebElement) ((org.openqa.selenium.JavascriptExecutor) driver)16 .executeScript("return arguments[0].shadowRoot", element);17 return ele;18 }19}

Full Screen

Full Screen

getShadowRoot

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.remote.RemoteWebElement;6import org.openqa.selenium.support.ui.ExpectedConditions;7import org.openqa.selenium.support.ui.WebDriverWait;8public class Main {9 public static void main(String[] args) {10 System.setProperty("webdriver.chrome.driver", "C:\\Users\\user\\Downloads\\chromedriver_win32\\chromedriver.exe");11 WebDriver driver = new ChromeDriver();12 driver.switchTo().frame("iframeResult");13 WebElement shadowRoot = expandRootElement(driver.findElement(By.tagName("html")));14 WebElement myElement = shadowRoot.findElement(By.id("demo"));15 System.out.println(myElement.getText());16 }17 public static WebElement expandRootElement(WebElement element) {18 WebElement ele = (WebElement) ((RemoteWebElement) element).getCoordinates().onPage();19 return ele;20 }21}

Full Screen

Full Screen

getShadowRoot

Using AI Code Generation

copy

Full Screen

1import org.openqa.selenium.By;2import org.openqa.selenium.JavascriptExecutor;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.chrome.ChromeDriver;6public class ShadowDOMHandling {7 public static void main(String[] args) throws InterruptedException {8 System.setProperty("webdriver.chrome.driver", "C:\\Users\\User\\Downloads\\chromedriver_win32\\chromedriver.exe");9 WebDriver driver = new ChromeDriver();10 driver.manage().window().maximize();11 Thread.sleep(10000);12 WebElement element = (WebElement) ((JavascriptExecutor) driver).executeScript(13 "return document.querySelector('books-app').shadowRoot.querySelector('books-drawer-layout').shadowRoot.querySelector('books-drawer').shadowRoot.querySelector('books-drawer-container').shadowRoot.querySelector('books-drawer-content').shadowRoot.querySelector('books-drawer-scrollable').shadowRoot.querySelector('books-drawer-items').shadowRoot.querySelector('books-drawer-item:nth-of-type(1)')");14 element.click();15 Thread.sleep(10000);16 driver.close();17 }18}

Full Screen

Full Screen

Selenium 4 Tutorial:

LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.

Chapters:

  1. Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.

  2. What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.

  3. Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.

  4. Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.

  5. How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.

  6. Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.

  7. Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.

Selenium 101 certifications:

LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful