Best Carina code snippet using com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement.doGetSelectedValue
Source:ExtendedWebElement.java
...1163 boolean doSelectByMatcher(final BaseMatcher<String> matcher);1164 boolean doSelectByPartialText(final String partialSelectText);1165 boolean doSelectByIndex(final int index);1166 1167 String doGetSelectedValue();1168 1169 List<String> doGetSelectedValues();1170 }1171 private Object executeAction(ACTION_NAME actionName, ActionSteps actionSteps, Object... inputArgs) {1172 Object result = null;1173 switch (actionName) {1174 case CLICK:1175 actionSteps.doClick();1176 break;1177 case CLICK_BY_JS:1178 actionSteps.doClickByJs();1179 break;1180 case CLICK_BY_ACTIONS:1181 actionSteps.doClickByActions();1182 break;1183 case DOUBLE_CLICK:1184 actionSteps.doDoubleClick();1185 break;1186 case HOVER:1187 actionSteps.doHover((Integer) inputArgs[0], (Integer) inputArgs[1]);1188 break;1189 case RIGHT_CLICK:1190 actionSteps.doRightClick();1191 break;1192 case GET_TEXT:1193 result = actionSteps.doGetText();1194 break;1195 case GET_LOCATION:1196 result = actionSteps.doGetLocation();1197 break;1198 case GET_SIZE:1199 result = actionSteps.doGetSize();1200 break;1201 case GET_ATTRIBUTE:1202 result = actionSteps.doGetAttribute((String) inputArgs[0]);1203 break;1204 case SEND_KEYS:1205 actionSteps.doSendKeys((Keys) inputArgs[0]);1206 break;1207 case TYPE:1208 actionSteps.doType((String) inputArgs[0]);1209 break;1210 case ATTACH_FILE:1211 actionSteps.doAttachFile((String) inputArgs[0]);1212 break;1213 case CHECK:1214 actionSteps.doCheck();1215 break;1216 case UNCHECK:1217 actionSteps.doUncheck();1218 break;1219 case IS_CHECKED:1220 result = actionSteps.doIsChecked();1221 break;1222 case SELECT:1223 result = actionSteps.doSelect((String) inputArgs[0]);1224 break;1225 case SELECT_VALUES:1226 result = actionSteps.doSelectValues((String[]) inputArgs);1227 break;1228 case SELECT_BY_MATCHER:1229 result = actionSteps.doSelectByMatcher((BaseMatcher<String>) inputArgs[0]);1230 break;1231 case SELECT_BY_PARTIAL_TEXT:1232 result = actionSteps.doSelectByPartialText((String) inputArgs[0]);1233 break;1234 case SELECT_BY_INDEX:1235 result = actionSteps.doSelectByIndex((int) inputArgs[0]);1236 break;1237 case GET_SELECTED_VALUE:1238 result = actionSteps.doGetSelectedValue();1239 break;1240 case GET_SELECTED_VALUES:1241 result = actionSteps.doGetSelectedValues();1242 break;1243 default:1244 Assert.fail("Unsupported UI action name" + actionName.toString());1245 break;1246 }1247 return result;1248 }1249 /**1250 * doAction on element.1251 *1252 * @param actionName1253 * ACTION_NAME1254 * @param timeout1255 * long1256 * @param waitCondition1257 * to check element conditions before action1258 * @return1259 * Object1260 */1261 private Object doAction(ACTION_NAME actionName, long timeout, ExpectedCondition<?> waitCondition) {1262 // [VD] do not remove null args otherwise all actions without arguments will be broken!1263 Object nullArgs = null;1264 return doAction(actionName, timeout, waitCondition, nullArgs);1265 }1266 private Object doAction(ACTION_NAME actionName, long timeout, ExpectedCondition<?> waitCondition,1267 Object...inputArgs) {1268 1269 if (waitCondition != null) {1270 //do verification only if waitCondition is not null1271 if (!waitUntil(waitCondition, timeout)) {1272 //TODO: think about raising exception otherwise we do extra call and might wait and hangs especially for mobile/appium1273 LOGGER.error(Messager.ELEMENT_CONDITION_NOT_VERIFIED.getMessage(actionName.getKey(), getNameWithLocator()));1274 }1275 }1276 1277 if (isLocalized) {1278 isLocalized = false; // single verification is enough for this particular element1279 L10N.verify(this);1280 }1281 Object output = null;1282 try {1283 this.element = getElement();1284 output = overrideAction(actionName, inputArgs);1285 } catch (StaleElementReferenceException e) {1286 //TODO: analyze mobile testing for staled elements. Potentially it should be fixed by appium java client already1287 // sometime Appium instead printing valid StaleElementException generate java.lang.ClassCastException:1288 // com.google.common.collect.Maps$TransformedEntriesMap cannot be cast to java.lang.String1289 LOGGER.debug("catched StaleElementReferenceException: ", e);1290 // try to find again using driver context and do action1291 element = this.findElement();1292 output = overrideAction(actionName, inputArgs);1293 }1294 return output;1295 }1296 // single place for all supported UI actions in carina core1297 private Object overrideAction(ACTION_NAME actionName, Object...inputArgs) {1298 Object output = executeAction(actionName, new ActionSteps() {1299 @Override1300 public void doClick() {1301 DriverListener.setMessages(Messager.ELEMENT_CLICKED.getMessage(getName()),1302 Messager.ELEMENT_NOT_CLICKED.getMessage(getNameWithLocator()));1303 element.click();1304 }1305 1306 @Override1307 public void doClickByJs() {1308 DriverListener.setMessages(Messager.ELEMENT_CLICKED.getMessage(getName()),1309 Messager.ELEMENT_NOT_CLICKED.getMessage(getNameWithLocator()));1310 LOGGER.info("Do click by JavascriptExecutor for element: " + getNameWithLocator());1311 JavascriptExecutor executor = (JavascriptExecutor) getDriver();1312 executor.executeScript("arguments[0].click();", element);1313 }1314 1315 @Override1316 public void doClickByActions() {1317 DriverListener.setMessages(Messager.ELEMENT_CLICKED.getMessage(getName()),1318 Messager.ELEMENT_NOT_CLICKED.getMessage(getNameWithLocator()));1319 LOGGER.info("Do click by Actions for element: " + getNameWithLocator());1320 Actions actions = new Actions(getDriver());1321 actions.moveToElement(element).click().perform();1322 } 1323 1324 @Override1325 public void doDoubleClick() {1326 DriverListener.setMessages(Messager.ELEMENT_DOUBLE_CLICKED.getMessage(getName()),1327 Messager.ELEMENT_NOT_DOUBLE_CLICKED.getMessage(getNameWithLocator()));1328 1329 WebDriver drv = getDriver();1330 Actions action = new Actions(drv);1331 action.moveToElement(element).doubleClick(element).build().perform();1332 }1333 1334 @Override1335 public void doHover(Integer xOffset, Integer yOffset) {1336 DriverListener.setMessages(Messager.ELEMENT_HOVERED.getMessage(getName()),1337 Messager.ELEMENT_NOT_HOVERED.getMessage(getNameWithLocator()));1338 1339 WebDriver drv = getDriver();1340 Actions action = new Actions(drv);1341 if (xOffset != null && yOffset!= null) {1342 action.moveToElement(element, xOffset, yOffset).build().perform();1343 } else {1344 action.moveToElement(element).build().perform();1345 }1346 }1347 1348 @Override1349 public void doSendKeys(Keys keys) {1350 DriverListener.setMessages(Messager.KEYS_SEND_TO_ELEMENT.getMessage(keys.toString(), getName()),1351 Messager.KEYS_NOT_SEND_TO_ELEMENT.getMessage(keys.toString(), getNameWithLocator()));1352 element.sendKeys(keys);1353 }1354 @Override1355 public void doType(String text) {1356 final String decryptedText = cryptoTool.decryptByPattern(text, CRYPTO_PATTERN);1357/* if (!element.getText().isEmpty()) {1358 DriverListener.setMessages(Messager.KEYS_CLEARED_IN_ELEMENT.getMessage(getName()),1359 Messager.KEYS_NOT_CLEARED_IN_ELEMENT.getMessage(getNameWithLocator()));1360 element.clear();1361 }1362*/1363 DriverListener.setMessages(Messager.KEYS_CLEARED_IN_ELEMENT.getMessage(getName()),1364 Messager.KEYS_NOT_CLEARED_IN_ELEMENT.getMessage(getNameWithLocator()));1365 element.clear();1366 String textLog = (!decryptedText.equals(text) ? "********" : text);1367 DriverListener.setMessages(Messager.KEYS_SEND_TO_ELEMENT.getMessage(textLog, getName()),1368 Messager.KEYS_NOT_SEND_TO_ELEMENT.getMessage(textLog, getNameWithLocator()));1369 element.sendKeys(decryptedText);1370 }1371 @Override1372 public void doAttachFile(String filePath) {1373 final String decryptedText = cryptoTool.decryptByPattern(filePath, CRYPTO_PATTERN);1374 String textLog = (!decryptedText.equals(filePath) ? "********" : filePath);1375 DriverListener.setMessages(Messager.FILE_ATTACHED.getMessage(textLog, getName()),1376 Messager.FILE_NOT_ATTACHED.getMessage(textLog, getNameWithLocator()));1377 ((JavascriptExecutor) getDriver()).executeScript("arguments[0].style.display = 'block';", element);1378 ((RemoteWebDriver) castDriver(getDriver())).setFileDetector(new LocalFileDetector());1379 element.sendKeys(decryptedText);1380 }1381 @Override1382 public String doGetText() {1383 String text = element.getText();1384 LOGGER.debug(Messager.ELEMENT_ATTRIBUTE_FOUND.getMessage("Text", text, getName()));1385 return text;1386 }1387 @Override1388 public Point doGetLocation() {1389 Point point = element.getLocation();1390 LOGGER.debug(Messager.ELEMENT_ATTRIBUTE_FOUND.getMessage("Location", point.toString(), getName()));1391 return point;1392 }1393 @Override1394 public Dimension doGetSize() {1395 Dimension dim = element.getSize();1396 LOGGER.debug(Messager.ELEMENT_ATTRIBUTE_FOUND.getMessage("Size", dim.toString(), getName()));1397 return dim;1398 }1399 @Override1400 public String doGetAttribute(String name) {1401 String attribute = element.getAttribute(name);1402 LOGGER.debug(Messager.ELEMENT_ATTRIBUTE_FOUND.getMessage(name, attribute, getName()));1403 return attribute;1404 }1405 @Override1406 public void doRightClick() {1407 DriverListener.setMessages(Messager.ELEMENT_RIGHT_CLICKED.getMessage(getName()),1408 Messager.ELEMENT_NOT_RIGHT_CLICKED.getMessage(getNameWithLocator()));1409 1410 WebDriver drv = getDriver();1411 Actions action = new Actions(drv);1412 action.moveToElement(element).contextClick(element).build().perform();1413 }1414 @Override1415 public void doCheck() {1416 DriverListener.setMessages(Messager.CHECKBOX_CHECKED.getMessage(getName()), null);1417 1418 boolean isSelected = element.isSelected();1419 if (element.getAttribute("checked") != null) {1420 isSelected |= element.getAttribute("checked").equalsIgnoreCase("true");1421 }1422 1423 if (!isSelected) {1424 click();1425 }1426 }1427 @Override1428 public void doUncheck() {1429 DriverListener.setMessages(Messager.CHECKBOX_UNCHECKED.getMessage(getName()), null);1430 1431 boolean isSelected = element.isSelected();1432 if (element.getAttribute("checked") != null) {1433 isSelected |= element.getAttribute("checked").equalsIgnoreCase("true");1434 }1435 1436 if (isSelected) {1437 click();1438 }1439 }1440 1441 @Override1442 public boolean doIsChecked() {1443 1444 boolean res = element.isSelected();1445 if (element.getAttribute("checked") != null) {1446 res |= element.getAttribute("checked").equalsIgnoreCase("true");1447 }1448 1449 return res;1450 }1451 1452 @Override1453 public boolean doSelect(String text) {1454 final String decryptedSelectText = cryptoTool.decryptByPattern(text, CRYPTO_PATTERN);1455 1456 String textLog = (!decryptedSelectText.equals(text) ? "********" : text);1457 1458 DriverListener.setMessages(Messager.SELECT_BY_TEXT_PERFORMED.getMessage(textLog, getName()),1459 Messager.SELECT_BY_TEXT_NOT_PERFORMED.getMessage(textLog, getNameWithLocator()));1460 1461 final Select s = new Select(getElement());1462 // [VD] do not use selectByValue as modern controls could have only visible value without value1463 s.selectByVisibleText(decryptedSelectText);1464 return true;1465 }1466 @Override1467 public boolean doSelectValues(String[] values) {1468 boolean result = true;1469 for (String value : values) {1470 if (!select(value)) {1471 result = false;1472 }1473 }1474 return result;1475 }1476 @Override1477 public boolean doSelectByMatcher(BaseMatcher<String> matcher) {1478 1479 DriverListener.setMessages(Messager.SELECT_BY_MATCHER_TEXT_PERFORMED.getMessage(matcher.toString(), getName()),1480 Messager.SELECT_BY_MATCHER_TEXT_NOT_PERFORMED.getMessage(matcher.toString(), getNameWithLocator()));1481 1482 final Select s = new Select(getElement());1483 String fullTextValue = null;1484 for (WebElement option : s.getOptions()) {1485 if (matcher.matches(option.getText())) {1486 fullTextValue = option.getText();1487 break;1488 }1489 }1490 s.selectByVisibleText(fullTextValue);1491 return true;1492 }1493 @Override1494 public boolean doSelectByPartialText(String partialSelectText) {1495 1496 DriverListener.setMessages(1497 Messager.SELECT_BY_TEXT_PERFORMED.getMessage(partialSelectText, getName()),1498 Messager.SELECT_BY_TEXT_NOT_PERFORMED.getMessage(partialSelectText, getNameWithLocator()));1499 1500 final Select s = new Select(getElement());1501 String fullTextValue = null;1502 for (WebElement option : s.getOptions()) {1503 if (option.getText().contains(partialSelectText)) {1504 fullTextValue = option.getText();1505 break;1506 }1507 }1508 s.selectByVisibleText(fullTextValue);1509 return true;1510 }1511 @Override1512 public boolean doSelectByIndex(int index) {1513 DriverListener.setMessages(1514 Messager.SELECT_BY_INDEX_PERFORMED.getMessage(String.valueOf(index), getName()),1515 Messager.SELECT_BY_INDEX_NOT_PERFORMED.getMessage(String.valueOf(index), getNameWithLocator()));1516 1517 1518 final Select s = new Select(getElement());1519 s.selectByIndex(index);1520 return true;1521 }1522 @Override1523 public String doGetSelectedValue() {1524 final Select s = new Select(getElement());1525 return s.getAllSelectedOptions().get(0).getText();1526 }1527 @Override1528 public List<String> doGetSelectedValues() {1529 final Select s = new Select(getElement());1530 List<String> values = new ArrayList<String>();1531 for (WebElement we : s.getAllSelectedOptions()) {1532 values.add(we.getText());1533 }1534 return values;1535 }1536 1537 }, inputArgs);1538 return output;1539 }1540 public WebDriver getDriver() {1541 if (driver == null) {1542 LOGGER.error("There is no any initialized driver for ExtendedWebElement: " + getNameWithLocator());...
doGetSelectedValue
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;2import org.openqa.selenium.support.FindBy;3public class TestPage extends AbstractPage {4 private ExtendedWebElement dropdown;5 public TestPage(WebDriver driver) {6 super(driver);7 }8 public String getDropdownValue() {9 return dropdown.doGetSelectedValue();10 }11 public String getDropdownValue2() {12 return dropdown.getSelectedValue();13 }14}15import org.openqa.selenium.WebDriver;16import org.openqa.selenium.support.ui.Select;17import org.testng.Assert;18import org.testng.annotations.Test;19public class TestPageTest extends AbstractTest {20 public void testGetDropdownValue() {21 TestPage testPage = new TestPage(getDriver());22 testPage.open();23 String dropdownValue = testPage.getDropdownValue();24 Assert.assertEquals(dropdownValue, "Value");25 }26 public void testGetDropdownValue2() {27 TestPage testPage = new TestPage(getDriver());28 testPage.open();29 String dropdownValue = testPage.getDropdownValue2();30 Assert.assertEquals(dropdownValue, "Value");31 }32}33import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;34import org.openqa.selenium.support.FindBy;35public class TestPage extends AbstractPage {36 private ExtendedWebElement dropdown;37 public TestPage(WebDriver driver) {38 super(driver);39 }40 public String getDropdownValue() {41 return dropdown.doGetSelectedValue();42 }43 public String getDropdownValue2() {44 return dropdown.getSelectedValue();45 }46}
doGetSelectedValue
Using AI Code Generation
1public String getSelectedValue() {2 return doGetSelectedValue();3}4public String getSelectedText() {5 return doGetSelectedText();6}7public void select(String option) {8 doSelect(option);9}10public void selectByIndex(int option) {11 doSelectByIndex(option);12}13public void selectByValue(String option) {14 doSelectByValue(option);15}16public void selectByVisibleText(String option) {17 doSelectByVisibleText(option);18}19public void deselect(String option) {20 doDeselect(option);21}22public void deselectByIndex(int option) {23 doDeselectByIndex(option);24}25public void deselectByValue(String option) {26 doDeselectByValue(option);27}28public void deselectByVisibleText(String option) {29 doDeselectByVisibleText(option);30}31public void deselectAll() {32 doDeselectAll();33}34public boolean isMultiple() {35 return doIsMultiple();36}
doGetSelectedValue
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;2import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;3import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;4public class Demo extends ExtendedWebElement {5 public static void main(String[] args) {6 ExtendedWebElement dropdown = new ExtendedWebElement();7 String selectedValue = dropdown.doGetSelectedValue();8 System.out.println("Selected value is: " + selectedValue);9 }10}
doGetSelectedValue
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;2import org.testng.annotations.Test;3public class GetSelectedValueFromDropDownList {4 public void getSelectedValueFromDropDownList() {5 String selectedValue = dropDownList.getSelectedValue();6 System.out.println("Selected value: " + selectedValue);7 }8}
doGetSelectedValue
Using AI Code Generation
1public class SelectListTest extends AbstractTest {2 public void testSelectList() {3 WebDriver driver = WebDriverFactory.getDriver();4 driver.switchTo().frame("iframeResult");5 String selectedValue = list.doGetSelectedValue();6 Assert.assertEquals(selectedValue, "USA", "Selected value is not correct");7 }8}9public class SelectListTest extends AbstractTest {10 public void testSelectList() {11 WebDriver driver = WebDriverFactory.getDriver();12 driver.switchTo().frame("iframeResult");13 list.doSelectByIndex(1);14 String selectedValue = list.doGetSelectedValue();15 Assert.assertEquals(selectedValue, "Germany", "Selected value is not correct");16 }17}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!