Best Citrus code snippet using com.consol.citrus.dsl.builder.SeleniumActionBuilder.argument
Source:SeleniumActionBuilder.java
...377 action.setAction(method);378 return this;379 }380 /**381 * Set page action argument.382 * @param arg383 * @return384 */385 public PageActionBuilder argument(String arg) {386 action.getArguments().add(arg);387 return this;388 }389 /**390 * Set page action arguments.391 * @param args392 * @return393 */394 public PageActionBuilder arguments(String ... args) {395 action.setArguments(Arrays.asList(args));396 return this;397 }398 /**399 * Set page action arguments.400 * @param args401 * @return402 */403 public PageActionBuilder arguments(List<String> args) {404 action.setArguments(args);405 return this;406 }407 @Override408 public TestAction build() {409 return SeleniumActionBuilder.this.build();410 }411 }412 /**413 * Customize javascript action.414 */415 public class JavaScriptActionBuilder implements TestActionBuilder {416 /** JavaScript action */417 private final JavaScriptAction action;418 /**419 * Default constructor.420 * @param action421 */422 public JavaScriptActionBuilder(JavaScriptAction action) {423 this.action = action;424 }425 /**426 * Add script argument.427 * @param arg428 * @return429 */430 public JavaScriptActionBuilder argument(Object arg) {431 action.getArguments().add(arg);432 return this;433 }434 /**435 * Add expected error.436 * @param errors437 * @return438 */439 public JavaScriptActionBuilder errors(String ... errors) {440 action.setExpectedErrors(Arrays.asList(errors));441 return this;442 }443 @Override444 public TestAction build() {...
Source:SeleniumTestRunnerTest.java
1/*2 * Copyright 2006-2017 the original author or authors.3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16package com.consol.citrus.dsl.runner;17import java.util.Collections;18import java.util.HashMap;19import java.util.HashSet;20import java.util.Set;21import com.consol.citrus.TestCase;22import com.consol.citrus.container.SequenceAfterTest;23import com.consol.citrus.container.SequenceBeforeTest;24import com.consol.citrus.context.TestContext;25import com.consol.citrus.dsl.UnitTestSupport;26import com.consol.citrus.dsl.builder.SeleniumActionBuilder;27import com.consol.citrus.report.TestActionListeners;28import com.consol.citrus.selenium.actions.AlertAction;29import com.consol.citrus.selenium.actions.CheckInputAction;30import com.consol.citrus.selenium.actions.ClearBrowserCacheAction;31import com.consol.citrus.selenium.actions.ClickAction;32import com.consol.citrus.selenium.actions.CloseWindowAction;33import com.consol.citrus.selenium.actions.FindElementAction;34import com.consol.citrus.selenium.actions.GetStoredFileAction;35import com.consol.citrus.selenium.actions.HoverAction;36import com.consol.citrus.selenium.actions.JavaScriptAction;37import com.consol.citrus.selenium.actions.NavigateAction;38import com.consol.citrus.selenium.actions.OpenWindowAction;39import com.consol.citrus.selenium.actions.SetInputAction;40import com.consol.citrus.selenium.actions.StartBrowserAction;41import com.consol.citrus.selenium.actions.StopBrowserAction;42import com.consol.citrus.selenium.actions.StoreFileAction;43import com.consol.citrus.selenium.actions.SwitchWindowAction;44import com.consol.citrus.selenium.actions.WaitUntilAction;45import com.consol.citrus.selenium.endpoint.SeleniumBrowser;46import com.consol.citrus.selenium.endpoint.SeleniumBrowserConfiguration;47import com.consol.citrus.selenium.endpoint.SeleniumHeaders;48import com.consol.citrus.spi.ReferenceResolver;49import org.mockito.Mockito;50import org.openqa.selenium.Alert;51import org.openqa.selenium.By;52import org.openqa.selenium.WebDriver;53import org.openqa.selenium.WebElement;54import org.openqa.selenium.chrome.ChromeDriver;55import org.openqa.selenium.interactions.Coordinates;56import org.openqa.selenium.interactions.Keyboard;57import org.openqa.selenium.interactions.Mouse;58import org.openqa.selenium.remote.BrowserType;59import org.openqa.selenium.remote.RemoteWebElement;60import org.testng.Assert;61import org.testng.annotations.Test;62import static org.mockito.ArgumentMatchers.anyString;63import static org.mockito.Mockito.verify;64import static org.mockito.Mockito.when;65/**66 * @author Christoph Deppisch67 * @since 2.768 */69public class SeleniumTestRunnerTest extends UnitTestSupport {70 private SeleniumBrowser seleniumBrowser = Mockito.mock(SeleniumBrowser.class);71 private SeleniumBrowserConfiguration seleniumBrowserConfiguration = Mockito.mock(SeleniumBrowserConfiguration.class);72 private ChromeDriver webDriver = Mockito.mock(ChromeDriver.class);73 private ReferenceResolver referenceResolver = Mockito.mock(ReferenceResolver.class);74 private WebElement element = Mockito.mock(WebElement.class);75 private WebElement button = Mockito.mock(WebElement.class);76 private RemoteWebElement link = Mockito.mock(RemoteWebElement.class);77 private WebElement input = Mockito.mock(WebElement.class);78 private WebElement checkbox = Mockito.mock(WebElement.class);79 private WebElement hidden = Mockito.mock(WebElement.class);80 private Alert alert = Mockito.mock(Alert.class);81 private WebDriver.Navigation navigation = Mockito.mock(WebDriver.Navigation.class);82 private WebDriver.TargetLocator locator = Mockito.mock(WebDriver.TargetLocator.class);83 private WebDriver.Options options = Mockito.mock(WebDriver.Options.class);84 private Mouse mouse = Mockito.mock(Mouse.class);85 private Keyboard keyboard = Mockito.mock(Keyboard.class);86 private Coordinates coordinates = Mockito.mock(Coordinates.class);87 @Test88 public void testSeleniumBuilder() {89 when(referenceResolver.resolve(TestContext.class)).thenReturn(applicationContext.getBean(TestContext.class));90 when(referenceResolver.resolve(TestActionListeners.class)).thenReturn(new TestActionListeners());91 when(referenceResolver.resolveAll(SequenceBeforeTest.class)).thenReturn(new HashMap<>());92 when(referenceResolver.resolveAll(SequenceAfterTest.class)).thenReturn(new HashMap<>());93 when(seleniumBrowser.getEndpointConfiguration()).thenReturn(seleniumBrowserConfiguration);94 when(seleniumBrowserConfiguration.getBrowserType()).thenReturn(BrowserType.CHROME);95 when(seleniumBrowser.getWebDriver()).thenReturn(webDriver);96 when(seleniumBrowser.getName()).thenReturn("mockBrowser");97 when(referenceResolver.resolve("mockBrowser", SeleniumBrowser.class)).thenReturn(seleniumBrowser);98 when(webDriver.navigate()).thenReturn(navigation);99 when(webDriver.manage()).thenReturn(options);100 when(webDriver.switchTo()).thenReturn(locator);101 when(locator.alert()).thenReturn(alert);102 when(alert.getText()).thenReturn("Hello!");103 when(webDriver.findElement(By.id("header"))).thenReturn(element);104 when(element.getTagName()).thenReturn("h1");105 when(element.isEnabled()).thenReturn(true);106 when(element.isDisplayed()).thenReturn(true);107 when(webDriver.findElement(By.linkText("Click Me!"))).thenReturn(link);108 when(link.getTagName()).thenReturn("a");109 when(link.isEnabled()).thenReturn(true);110 when(link.isDisplayed()).thenReturn(true);111 when(webDriver.findElement(By.linkText("Hover Me!"))).thenReturn(link);112 when(webDriver.getMouse()).thenReturn(mouse);113 when(webDriver.getKeyboard()).thenReturn(keyboard);114 when(link.getCoordinates()).thenReturn(coordinates);115 when(webDriver.findElement(By.name("username"))).thenReturn(input);116 when(input.getTagName()).thenReturn("input");117 when(input.isEnabled()).thenReturn(true);118 when(input.isDisplayed()).thenReturn(true);119 when(webDriver.findElement(By.name("hiddenButton"))).thenReturn(hidden);120 when(hidden.getTagName()).thenReturn("input");121 when(hidden.isEnabled()).thenReturn(true);122 when(hidden.isDisplayed()).thenReturn(false);123 when(webDriver.findElement(By.xpath("//input[@type='checkbox']"))).thenReturn(checkbox);124 when(checkbox.getTagName()).thenReturn("input");125 when(checkbox.isEnabled()).thenReturn(true);126 when(checkbox.isDisplayed()).thenReturn(true);127 when(checkbox.isSelected()).thenReturn(false);128 when(webDriver.executeScript(anyString())).thenReturn(Collections.singletonList("This went wrong!"));129 when(webDriver.findElement(By.className("btn"))).thenReturn(button);130 when(button.getTagName()).thenReturn("button");131 when(button.isEnabled()).thenReturn(false);132 when(button.isDisplayed()).thenReturn(false);133 when(button.getText()).thenReturn("Click Me!");134 when(button.getAttribute("type")).thenReturn("submit");135 when(button.getCssValue("color")).thenReturn("red");136 when(seleniumBrowser.getStoredFile("file.txt")).thenReturn("file.txt");137 Set<String> windows = new HashSet<>();138 windows.add("last_window");139 windows.add("new_window");140 when(webDriver.getWindowHandles()).thenReturn(Collections.singleton("last_window")).thenReturn(windows);141 when(webDriver.getWindowHandle()).thenReturn("last_window");142 context.setVariable("cssClass", "btn");143 context.setReferenceResolver(referenceResolver);144 MockTestRunner builder = new MockTestRunner(getClass().getSimpleName(), context) {145 @Override146 public void execute() {147 selenium(action -> action.start(seleniumBrowser));148 selenium(action -> action.navigate("http://localhost:9090"));149 selenium(action -> action.find().element(By.id("header")));150 selenium(action -> action.find().element("class-name", "${cssClass}")151 .tagName("button")152 .enabled(false)153 .displayed(false)154 .text("Click Me!")155 .style("color", "red")156 .attribute("type", "submit"));157 selenium(action -> action.click().element(By.linkText("Click Me!")));158 selenium(action -> action.hover().element(By.linkText("Hover Me!")));159 selenium(action -> action.setInput("Citrus").element(By.name("username")));160 selenium(action -> action.checkInput(false).element(By.xpath("//input[@type='checkbox']")));161 selenium(action -> action.javascript("alert('Hello!')")162 .errors("This went wrong!"));163 selenium(action -> action.alert().text("Hello!").accept());164 selenium(SeleniumActionBuilder::clearCache);165 selenium(action -> action.store("classpath:download/file.txt"));166 selenium(action -> action.getStored("file.txt"));167 selenium(action -> action.open().window("my_window"));168 selenium(action -> action.focus().window("my_window"));169 selenium(action -> action.close().window("my_window"));170 selenium(action -> action.waitUntil().hidden().element(By.name("hiddenButton")));171 selenium(action -> action.stop(seleniumBrowser));172 }173 };174 TestCase test = builder.getTestCase();175 int actionIndex = 0;176 Assert.assertEquals(test.getActionCount(), 18);177 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), StartBrowserAction.class);178 StartBrowserAction startBrowserAction = (StartBrowserAction) test.getActions().get(actionIndex++);179 Assert.assertEquals(startBrowserAction.getName(), "selenium:start");180 Assert.assertNotNull(startBrowserAction.getBrowser());181 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), NavigateAction.class);182 NavigateAction navigateAction = (NavigateAction) test.getActions().get(actionIndex++);183 Assert.assertEquals(navigateAction.getName(), "selenium:navigate");184 Assert.assertEquals(navigateAction.getPage(), "http://localhost:9090");185 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), FindElementAction.class);186 FindElementAction findElementAction = (FindElementAction) test.getActions().get(actionIndex++);187 Assert.assertEquals(findElementAction.getName(), "selenium:find");188 Assert.assertEquals(findElementAction.getBy(), By.id("header"));189 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), FindElementAction.class);190 findElementAction = (FindElementAction) test.getActions().get(actionIndex++);191 Assert.assertEquals(findElementAction.getName(), "selenium:find");192 Assert.assertEquals(findElementAction.getProperty(), "class-name");193 Assert.assertEquals(findElementAction.getPropertyValue(), "${cssClass}");194 Assert.assertEquals(findElementAction.getTagName(), "button");195 Assert.assertEquals(findElementAction.getText(), "Click Me!");196 Assert.assertFalse(findElementAction.isEnabled());197 Assert.assertFalse(findElementAction.isDisplayed());198 Assert.assertEquals(findElementAction.getStyles().size(), 1L);199 Assert.assertEquals(findElementAction.getStyles().get("color"), "red");200 Assert.assertEquals(findElementAction.getAttributes().size(), 1L);201 Assert.assertEquals(findElementAction.getAttributes().get("type"), "submit");202 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), ClickAction.class);203 ClickAction clickAction = (ClickAction) test.getActions().get(actionIndex++);204 Assert.assertEquals(clickAction.getName(), "selenium:click");205 Assert.assertEquals(clickAction.getBy(), By.linkText("Click Me!"));206 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), HoverAction.class);207 HoverAction hoverAction = (HoverAction) test.getActions().get(actionIndex++);208 Assert.assertEquals(hoverAction.getName(), "selenium:hover");209 Assert.assertEquals(hoverAction.getBy(), By.linkText("Hover Me!"));210 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), SetInputAction.class);211 SetInputAction setInputAction = (SetInputAction) test.getActions().get(actionIndex++);212 Assert.assertEquals(setInputAction.getName(), "selenium:set-input");213 Assert.assertEquals(setInputAction.getBy(), By.name("username"));214 Assert.assertEquals(setInputAction.getValue(), "Citrus");215 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), CheckInputAction.class);216 CheckInputAction checkInputAction = (CheckInputAction) test.getActions().get(actionIndex++);217 Assert.assertEquals(checkInputAction.getName(), "selenium:check-input");218 Assert.assertEquals(checkInputAction.getBy(), By.xpath("//input[@type='checkbox']"));219 Assert.assertFalse(checkInputAction.isChecked());220 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), JavaScriptAction.class);221 JavaScriptAction javaScriptAction = (JavaScriptAction) test.getActions().get(actionIndex++);222 Assert.assertEquals(javaScriptAction.getName(), "selenium:javascript");223 Assert.assertEquals(javaScriptAction.getScript(), "alert('Hello!')");224 Assert.assertEquals(javaScriptAction.getExpectedErrors().size(), 1L);225 Assert.assertEquals(javaScriptAction.getExpectedErrors().get(0), "This went wrong!");226 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), AlertAction.class);227 AlertAction alertAction = (AlertAction) test.getActions().get(actionIndex++);228 Assert.assertEquals(alertAction.getName(), "selenium:alert");229 Assert.assertEquals(alertAction.getText(), "Hello!");230 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), ClearBrowserCacheAction.class);231 ClearBrowserCacheAction clearBrowserCacheAction = (ClearBrowserCacheAction) test.getActions().get(actionIndex++);232 Assert.assertEquals(clearBrowserCacheAction.getName(), "selenium:clear-cache");233 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), StoreFileAction.class);234 StoreFileAction storeFileAction = (StoreFileAction) test.getActions().get(actionIndex++);235 Assert.assertEquals(storeFileAction.getName(), "selenium:store-file");236 Assert.assertEquals(storeFileAction.getFilePath(), "classpath:download/file.txt");237 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), GetStoredFileAction.class);238 GetStoredFileAction getStoredFileAction = (GetStoredFileAction) test.getActions().get(actionIndex++);239 Assert.assertEquals(getStoredFileAction.getName(), "selenium:get-stored-file");240 Assert.assertEquals(getStoredFileAction.getFileName(), "file.txt");241 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), OpenWindowAction.class);242 OpenWindowAction openWindowAction = (OpenWindowAction) test.getActions().get(actionIndex++);243 Assert.assertEquals(openWindowAction.getName(), "selenium:open-window");244 Assert.assertEquals(openWindowAction.getWindowName(), "my_window");245 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), SwitchWindowAction.class);246 SwitchWindowAction switchWindowAction = (SwitchWindowAction) test.getActions().get(actionIndex++);247 Assert.assertEquals(switchWindowAction.getName(), "selenium:switch-window");248 Assert.assertEquals(switchWindowAction.getWindowName(), "my_window");249 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), CloseWindowAction.class);250 CloseWindowAction closeWindowAction = (CloseWindowAction) test.getActions().get(actionIndex++);251 Assert.assertEquals(closeWindowAction.getName(), "selenium:close-window");252 Assert.assertEquals(closeWindowAction.getWindowName(), "my_window");253 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), WaitUntilAction.class);254 WaitUntilAction waitUntilAction = (WaitUntilAction) test.getActions().get(actionIndex++);255 Assert.assertEquals(waitUntilAction.getName(), "selenium:wait");256 Assert.assertEquals(waitUntilAction.getBy(), By.name("hiddenButton"));257 Assert.assertEquals(waitUntilAction.getCondition(), "hidden");258 Assert.assertEquals(test.getActions().get(actionIndex).getClass(), StopBrowserAction.class);259 StopBrowserAction stopBrowserAction = (StopBrowserAction) test.getActions().get(actionIndex);260 Assert.assertEquals(stopBrowserAction.getName(), "selenium:stop");261 Assert.assertNotNull(stopBrowserAction.getBrowser());262 Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_ALERT_TEXT), "Hello!");263 Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_DOWNLOAD_FILE), "file.txt");264 Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_LAST_WINDOW), "last_window");265 Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW), "new_window");266 Assert.assertEquals(context.getVariable("my_window"), "new_window");267 verify(alert).accept();268 verify(options).deleteAllCookies();269 verify(link).click();270 verify(input).clear();271 verify(input).sendKeys("Citrus");272 }273}...
Source:TodoPagesIT.java
...82 .page(todoPage)83 .validate());84 selenium(seleniumActionBuilder -> seleniumActionBuilder85 .page(todoPage)86 .argument("${todoName}")87 .argument("${todoDescription}")88 .execute("submit"));89 selenium(seleniumActionBuilder -> seleniumActionBuilder90 .waitUntil()91 .element(By.xpath("(//li[@class='list-group-item']/span)[last()]"))92 .timeout(2000L)93 .visible());94 selenium(seleniumActionBuilder -> seleniumActionBuilder95 .find()96 .element(By.xpath("(//li[@class='list-group-item']/span)[last()]"))97 .text("${todoName}"));98 }99}...
argument
Using AI Code Generation
1public class 3 extends TestNGCitrusTestDesigner {2 public void 3() {3 selenium().type("q", "citrus:concat('citrus ', citrus:randomNumber(4))");4 selenium().click("btnG");5 selenium().verifyText().element(By.id("resultStats")).text("citrus:startsWith('About')");6 }7}8public class 4 extends TestNGCitrusTestDesigner {9 public void 4() {10 selenium().type("q", "citrus:concat('citrus ', citrus:randomNumber(4))");11 selenium().click("btnG");12 selenium().verifyText().element(By.id("resultStats")).text("citrus:startsWith('About')");13 }14}15public class 5 extends TestNGCitrusTestDesigner {16 public void 5() {17 selenium().type("q", "citrus:concat('citrus ', citrus:randomNumber(4))");18 selenium().click("btnG");19 selenium().verifyText().element(By.id("resultStats")).text("citrus:startsWith('About')");20 }21}22public class 6 extends TestNGCitrusTestDesigner {23 public void 6() {24 selenium().type("q", "citrus:concat('citrus ', citrus:randomNumber(4))");25 selenium().click("btnG");26 selenium().verifyText().element(By.id("resultStats")).text("citrus:startsWith('About')");27 }28}29public class 7 extends TestNGCitrusTestDesigner {
argument
Using AI Code Generation
1public class 3 extends TestNGCitrusTestDesigner {2 public void 3() {3 selenium().click("id=login");4 selenium().type("id=username", "admin");5 selenium().type("id=password", "password");6 selenium().click("id=submit");7 selenium().click("id=logout");8 }9}
argument
Using AI Code Generation
1public class 3 extends TestNGCitrusTestDesigner {2 public void 3() {3 selenium().action().type("q", "Citrus");4 selenium().action().click("btnG");5 selenium().action().waitForPageToLoad("30000");6 selenium().action().verifyTextPresent("Citrus");7 }8}9public class 4 extends TestNGCitrusTestDesigner {10 public void 4() {11 selenium().action().type("q", "Citrus");12 selenium().action().click("btnG");13 selenium().action().waitForPageToLoad("30000");14 selenium().action().verifyTextPresent("Citrus");15 }16}17public class 5 extends TestNGCitrusTestDesigner {18 public void 5() {19 selenium().action().type("q", "Citrus");20 selenium().action().click("btnG");21 selenium().action().waitForPageToLoad("30000");22 selenium().action().verifyTextPresent("Citrus");23 }24}25public class 6 extends TestNGCitrusTestDesigner {26 public void 6() {27 selenium().action().type("q", "Citrus");28 selenium().action().click("btnG");29 selenium().action().waitForPageToLoad("30000");30 selenium().action().verifyTextPresent("Citrus");31 }32}
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!!