Best Citrus code snippet using com.consol.citrus.selenium.actions.FindElementAction.createBy
Source:FindElementAction.java
...58 super(name);59 }60 @Override61 protected final void execute(SeleniumBrowser browser, TestContext context) {62 WebElement element = browser.getWebDriver().findElement(createBy(context));63 if (element == null) {64 throw new CitrusRuntimeException(String.format("Failed to find element '%s' on page", property + "=" + propertyValue));65 }66 validate(element, browser, context);67 execute(element, browser, context);68 }69 /**70 * Validates found web element with expected content.71 * @param element72 * @param browser73 * @param context74 */75 protected void validate(WebElement element, SeleniumBrowser browser, TestContext context) {76 validateElementProperty("tag-name", tagName, element.getTagName(), context);77 validateElementProperty("text", text, element.getText(), context);78 Assert.isTrue(displayed == element.isDisplayed(), String.format("Selenium web element validation failed, " +79 "property 'displayed' expected '%s', but was '%s'", displayed, element.isDisplayed()));80 Assert.isTrue(enabled == element.isEnabled(), String.format("Selenium web element validation failed, " +81 "property 'enabled' expected '%s', but was '%s'", enabled, element.isEnabled()));82 for (Map.Entry<String, String> attributeEntry : attributes.entrySet()) {83 validateElementProperty(String.format("attribute '%s'", attributeEntry.getKey()), attributeEntry.getValue(), element.getAttribute(attributeEntry.getKey()), context);84 }85 for (Map.Entry<String, String> styleEntry : styles.entrySet()) {86 validateElementProperty(String.format("css style '%s'", styleEntry.getKey()), styleEntry.getValue(), element.getCssValue(styleEntry.getKey()), context);87 }88 }89 /**90 * Validates web element property value with validation matcher support.91 * @param propertyName92 * @param controlValue93 * @param resultValue94 * @param context95 */96 private void validateElementProperty(String propertyName, String controlValue, String resultValue, TestContext context) {97 if (StringUtils.hasText(controlValue)) {98 String control = context.replaceDynamicContentInString(controlValue);99 if (ValidationMatcherUtils.isValidationMatcherExpression(control)) {100 ValidationMatcherUtils.resolveValidationMatcher("payload", resultValue, control, context);101 } else {102 Assert.isTrue(control.equals(resultValue), String.format("Selenium web element validation failed, %s expected '%s', but was '%s'", propertyName, control, resultValue));103 }104 }105 }106 /**107 * Subclasses may override this method in order to add element actions.108 * @param element109 * @param browser110 * @param context111 */112 protected void execute(WebElement element, SeleniumBrowser browser, TestContext context) {113 if (StringUtils.hasText(element.getTagName())) {114 context.setVariable(element.getTagName(), element);115 }116 }117 /**118 * Create by selector from type information.119 * @return120 */121 protected By createBy(TestContext context) {122 if (by != null) {123 return by;124 }125 switch (property) {126 case "id":127 return By.id(context.replaceDynamicContentInString(propertyValue));128 case "class-name":129 return By.className(context.replaceDynamicContentInString(propertyValue));130 case "link-text":131 return By.linkText(context.replaceDynamicContentInString(propertyValue));132 case "css-selector":133 return By.cssSelector(context.replaceDynamicContentInString(propertyValue));134 case "name":135 return By.name(context.replaceDynamicContentInString(propertyValue));...
createBy
Using AI Code Generation
1import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;2import com.consol.citrus.selenium.actions.NavigateToAction;3import com.consol.citrus.selenium.actions.FindElementAction;4import com.consol.citrus.selenium.endpoint.SeleniumBrowser;5import com.consol.citrus.selenium.endpoint.SeleniumHeaders;6import org.openqa.selenium.By;7import org.openqa.selenium.WebElement;8import org.springframework.beans.factory.annotation.Autowired;9import org.springframework.http.HttpMethod;10import org.springframework.http.HttpStatus;11import org.springframework.http.MediaType;12import org.testng.annotations.Test;13public class SeleniumJavaTest extends TestNGCitrusTestDesigner {14 private SeleniumBrowser browser;15 public void test() {16 variable("searchTerm", "citrus:concat('citrus:', citrus:randomNumber(4))");17 selenium(action -> action18 .browser(browser)19 .start());20 selenium(action -> action21 .browser(browser)22 selenium(action -> action23 .browser(browser)24 .findElement(By.name("q"))25 .sendKeys("${searchTerm}"));26 selenium(action -> action27 .browser(browser)28 .findElement(By.name("btnK"))29 .click());30 selenium(action -> action31 .browser(browser)32 .findElement(By.id("resultStats"))33 .getText());34 selenium(action -> action35 .browser(browser)36 .stop());37 }38}
createBy
Using AI Code Generation
1String var1 = “abc”;2int var2 = 10;3double var3 = 10.0;4boolean var4 = true;5char var5 = ‘a’;6long var6 = 1000000000000000000L;7float var7 = 10.0f;8byte var8 = 10;9short var9 = 10;10Object var10 = new Object();11var var11 = “abc”;12var var12 = 10;13var var13 = 10.0;14var var14 = true;
createBy
Using AI Code Generation
1public class FindElementActionTest extends AbstractSeleniumTest {2 public void findElementAction() {3 variable("element", "id=foo");4 variable("value", "bar");5 selenium().createBy("id");6 selenium().findElement("${element}").setValue("${value}");7 }8}9public void findElementAction() {10 selenium().findElement("id=foo").click();11}12public void findElementAction() {13 selenium().findElement("name=foo").click();14}15public void findElementAction() {16 selenium().findElement("class=foo").click();17}
createBy
Using AI Code Generation
1FindElementAction findElementAction = new FindElementAction.Builder()2 .locator(createBy("css", "input#input"))3 .build();4findElementAction.execute(context);5WebElement element = findElementAction.getElement();6FindElementsAction findElementsAction = new FindElementsAction.Builder()7 .locator(createBy("css", "input#input"))8 .build();9findElementsAction.execute(context);10List<WebElement> elements = findElementsAction.getElements();11ClickAction clickAction = new ClickAction.Builder()12 .locator(createBy("css", "input#input"))13 .build();14clickAction.execute(context);15SendKeysAction sendKeysAction = new SendKeysAction.Builder()16 .locator(createBy("css", "input#input"))17 .text("Hello World!")18 .build();19sendKeysAction.execute(context);20SelectAction selectAction = new SelectAction.Builder()21 .locator(createBy("css", "select#select"))22 .option("option1")23 .build();24selectAction.execute(context);25ClearAction clearAction = new ClearAction.Builder()26 .locator(createBy("css", "input#input"))27 .build();28clearAction.execute(context);29CloseAction closeAction = new CloseAction.Builder()30 .build();31closeAction.execute(context);32QuitAction quitAction = new QuitAction.Builder()33 .build();34quitAction.execute(context);35SwitchToWindowAction switchToWindowAction = new SwitchToWindowAction.Builder()36 .locator(createBy("css", "input#input"))37 .build();38switchToWindowAction.execute(context);
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!!