Best Citrus code snippet using com.consol.citrus.cucumber.step.designer.selenium.SeleniumSteps.page
Source:SeleniumSteps.java
...39 protected TestDesigner designer;40 @CitrusFramework41 protected Citrus citrus;42 /** Page objects defined by id */43 private Map<String, WebPage> pages;44 /** Page validators defined by id */45 private Map<String, PageValidator> validators;46 /** Selenium browser */47 protected SeleniumBrowser browser;48 @Before49 public void before(Scenario scenario) {50 if (browser == null && citrus.getApplicationContext().getBeansOfType(SeleniumBrowser.class).size() == 1L) {51 browser = citrus.getApplicationContext().getBean(SeleniumBrowser.class);52 }53 pages = new HashMap<>();54 validators = new HashMap<>();55 }56 @Given("^(?:selenium )?browser \"([^\"]+)\"$")57 public void setBrowser(String id) {58 if (!citrus.getApplicationContext().containsBean(id)) {59 throw new CitrusRuntimeException("Unable to find selenium browser for id: " + id);60 }61 browser = citrus.getApplicationContext().getBean(id, SeleniumBrowser.class);62 }63 @Given("^pages$")64 public void pages(DataTable dataTable) {65 Map<String, String> variables = dataTable.asMap(String.class, String.class);66 for (Map.Entry<String, String> entry : variables.entrySet()) {67 page(entry.getKey(), entry.getValue());68 }69 }70 @Given("^page \"([^\"]+)\" ([^\\s]+)$")71 public void page(String id, String type) {72 try {73 pages.put(id, (WebPage) Class.forName(type).newInstance());74 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {75 throw new CitrusRuntimeException("Failed to laod page object", e);76 }77 }78 @Given("^page validators")79 public void page_validators(DataTable dataTable) {80 Map<String, String> variables = dataTable.asMap(String.class, String.class);81 for (Map.Entry<String, String> entry : variables.entrySet()) {82 page_validator(entry.getKey(), entry.getValue());83 }84 }85 @Given("^page validator ([^\\s]+) ([^\\s]+)$")86 public void page_validator(String id, String type) {87 try {88 validators.put(id, (PageValidator) Class.forName(type).newInstance());89 } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {90 throw new CitrusRuntimeException("Failed to laod page object", e);91 }92 }93 @When("^(?:user )?starts? browser$")94 public void start() {95 designer.selenium().browser(browser)96 .start();97 }98 @When("^(?:user )?stops? browser$")99 public void stop() {100 designer.selenium().browser(browser)101 .stop();102 }103 @When("^(?:user )?navigates? to \"([^\"]+)\"$")104 public void navigate(String url) {105 designer.selenium().browser(browser)106 .navigate(url);107 }108 @When("^(?:user )?clicks? (?:element|button|link) with ([^\"]+)=\"([^\"]+)\"$")109 public void click(String property, String value) {110 designer.selenium().browser(browser)111 .click()112 .element(property, value);113 }114 @When("^(?:user )?(?:sets?|puts?) text \"([^\"]+)\" to (?:element|input|textfield) with ([^\"]+)=\"([^\"]+)\"$")115 public void setInput(String text, String property, String value) {116 designer.selenium().browser(browser)117 .setInput(text)118 .element(property, value);119 }120 @When("^(?:user )?(checks?|unchecks?) checkbox with ([^\"]+)=\"([^\"]+)\"$")121 public void checkInput(String type, String property, String value) {122 designer.selenium().browser(browser)123 .checkInput(type.equals("check") || type.equals("checks"))124 .element(property, value);125 }126 @Then("^(?:page )?should (?:display|have) (?:element|button|link|input|textfield|form|heading) with (id|name|class-name|link-text|css-selector|tag-name|xpath)=\"([^\"]+)\"$")127 public void should_display(String property, String value) {128 designer.selenium().browser(browser)129 .find()130 .enabled(true)131 .displayed(true)132 .element(property, value);133 }134 @Then("^(?:page )?should (?:display|have) (?:element|button|link|input|textfield|form|heading) with (id|name|class-name|link-text|css-selector|tag-name|xpath)=\"([^\"]+)\" having$")135 public void should_display(String property, String value, DataTable dataTable) {136 Map<String, String> elementProperties = dataTable.asMap(String.class, String.class);137 SeleniumActionBuilder.FindElementActionBuilder elementBuilder = designer.selenium().browser(browser)138 .find()139 .element(property, value);140 for (Map.Entry<String, String> propertyEntry : elementProperties.entrySet()) {141 if (propertyEntry.getKey().equals("tag-name")) {142 elementBuilder.tagName(propertyEntry.getValue());143 }144 if (propertyEntry.getKey().equals("text")) {145 elementBuilder.text(propertyEntry.getValue());146 }147 if (propertyEntry.getKey().equals("enabled")) {148 elementBuilder.enabled(Boolean.valueOf(propertyEntry.getValue()));149 }150 if (propertyEntry.getKey().equals("displayed")) {151 elementBuilder.displayed(Boolean.valueOf(propertyEntry.getValue()));152 }153 if (propertyEntry.getKey().equals("styles") || propertyEntry.getKey().equals("style")) {154 String[] propertyExpressions = StringUtils.delimitedListToStringArray(propertyEntry.getValue(), ";");155 for (String propertyExpression : propertyExpressions) {156 String[] keyValue = propertyExpression.split("=");157 elementBuilder.style(keyValue[0].trim(), VariableUtils.cutOffDoubleQuotes(keyValue[1].trim()));158 }159 }160 if (propertyEntry.getKey().equals("attributes") || propertyEntry.getKey().equals("attribute")) {161 String[] propertyExpressions = StringUtils.commaDelimitedListToStringArray(propertyEntry.getValue());162 for (String propertyExpression : propertyExpressions) {163 String[] keyValue = propertyExpression.split("=");164 elementBuilder.attribute(keyValue[0].trim(), VariableUtils.cutOffDoubleQuotes(keyValue[1].trim()));165 }166 }167 }168 }169 @When("^(?:page )?([^\\s]+) performs ([^\\s]+)$")170 public void page_action(String pageId, String method) {171 page_action_with_arguments(pageId, method, null);172 }173 @When("^(?:page )?([^\\s]+) performs ([^\\s]+) with arguments$")174 public void page_action_with_arguments(String pageId, String method, DataTable dataTable) {175 verifyPage(pageId);176 List<String> arguments = new ArrayList<>();177 if (dataTable != null) {178 arguments = dataTable.asList(String.class);179 }180 designer.selenium().browser(browser)181 .page(pages.get(pageId))182 .execute(method)183 .arguments(arguments);184 }185 @Then("^(?:page )?([^\\s]+) should validate$")186 public void page_should_validate(String pageId) {187 page_should_validate_with_validator(pageId, null);188 }189 @Then("^(?:page )?([^\\s]+) should validate with ([^\\s]+)$")190 public void page_should_validate_with_validator(String pageId, String validatorId) {191 verifyPage(pageId);192 PageValidator pageValidator = null;193 if (validators.containsKey(validatorId)) {194 pageValidator = validators.get(validatorId);195 }196 designer.selenium().browser(browser)197 .page(pages.get(pageId))198 .validator(pageValidator)199 .validate();200 }201 /**202 * Verify that page is known.203 * @param pageId204 */205 private void verifyPage(String pageId) {206 if (!pages.containsKey(pageId)) {207 throw new CitrusRuntimeException(String.format("Unknown page '%s' - please introduce page with type information first", pageId));208 }209 }210}...
Source:SeleniumStepsTest.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.cucumber.step.designer.selenium;17import com.consol.citrus.Citrus;18import com.consol.citrus.annotations.CitrusAnnotations;19import com.consol.citrus.dsl.actions.DelegatingTestAction;20import com.consol.citrus.dsl.annotations.CitrusDslAnnotations;21import com.consol.citrus.dsl.design.DefaultTestDesigner;22import com.consol.citrus.dsl.design.TestDesigner;23import com.consol.citrus.selenium.actions.*;24import com.consol.citrus.selenium.endpoint.SeleniumBrowser;25import com.consol.citrus.testng.AbstractTestNGUnitTest;26import cucumber.api.Scenario;27import org.mockito.Mockito;28import org.springframework.beans.factory.annotation.Autowired;29import org.testng.Assert;30import org.testng.annotations.*;31/**32 * @author Christoph Deppisch33 * @since 2.734 */35public class SeleniumStepsTest extends AbstractTestNGUnitTest {36 private Citrus citrus;37 private SeleniumSteps steps;38 private TestDesigner designer;39 @Autowired40 private SeleniumBrowser seleniumBrowser;41 @BeforeClass42 public void setup() {43 citrus = Citrus.newInstance(applicationContext);44 }45 @BeforeMethod46 public void injectResources() {47 steps = new SeleniumSteps();48 designer = new DefaultTestDesigner(applicationContext, context);49 CitrusAnnotations.injectAll(steps, citrus, context);50 CitrusDslAnnotations.injectTestDesigner(steps, designer);51 }52 @Test53 public void testStart() {54 steps.setBrowser("seleniumBrowser");55 steps.start();56 Assert.assertEquals(designer.getTestCase().getActionCount(), 1L);57 Assert.assertTrue(((DelegatingTestAction) designer.getTestCase().getTestAction(0)).getDelegate() instanceof SeleniumAction);58 SeleniumAction action = (SeleniumAction) ((DelegatingTestAction) designer.getTestCase().getTestAction(0)).getDelegate();59 Assert.assertEquals(action.getBrowser(), seleniumBrowser);60 Assert.assertTrue(action instanceof StartBrowserAction);61 }62 @Test63 public void testStop() {64 steps.setBrowser("seleniumBrowser");65 steps.stop();66 Assert.assertEquals(designer.getTestCase().getActionCount(), 1L);67 Assert.assertTrue(((DelegatingTestAction) designer.getTestCase().getTestAction(0)).getDelegate() instanceof SeleniumAction);68 SeleniumAction action = (SeleniumAction) ((DelegatingTestAction) designer.getTestCase().getTestAction(0)).getDelegate();69 Assert.assertEquals(action.getBrowser(), seleniumBrowser);70 Assert.assertTrue(action instanceof StopBrowserAction);71 }72 @Test73 public void testNavigate() {74 steps.setBrowser("seleniumBrowser");75 steps.navigate("http://localhost:8080/test");76 Assert.assertEquals(designer.getTestCase().getActionCount(), 1L);77 Assert.assertTrue(((DelegatingTestAction) designer.getTestCase().getTestAction(0)).getDelegate() instanceof SeleniumAction);78 SeleniumAction action = (SeleniumAction) ((DelegatingTestAction) designer.getTestCase().getTestAction(0)).getDelegate();79 Assert.assertEquals(action.getBrowser(), seleniumBrowser);80 Assert.assertTrue(action instanceof NavigateAction);81 Assert.assertEquals(((NavigateAction)action).getPage(), "http://localhost:8080/test");82 }83 @Test84 public void testClick() {85 steps.setBrowser("seleniumBrowser");86 steps.click("id", "foo");87 Assert.assertEquals(designer.getTestCase().getActionCount(), 1L);88 Assert.assertTrue(((DelegatingTestAction) designer.getTestCase().getTestAction(0)).getDelegate() instanceof SeleniumAction);89 SeleniumAction action = (SeleniumAction) ((DelegatingTestAction) designer.getTestCase().getTestAction(0)).getDelegate();90 Assert.assertEquals(action.getBrowser(), seleniumBrowser);91 Assert.assertTrue(action instanceof ClickAction);92 Assert.assertEquals(((ClickAction)action).getProperty(), "id");93 Assert.assertEquals(((ClickAction)action).getPropertyValue(), "foo");94 }95 96 @Test97 public void testSetInput() {98 steps.setBrowser("seleniumBrowser");99 steps.setInput("Hello","id", "foo");100 Assert.assertEquals(designer.getTestCase().getActionCount(), 1L);101 Assert.assertTrue(((DelegatingTestAction) designer.getTestCase().getTestAction(0)).getDelegate() instanceof SeleniumAction);102 SeleniumAction action = (SeleniumAction) ((DelegatingTestAction) designer.getTestCase().getTestAction(0)).getDelegate();103 Assert.assertEquals(action.getBrowser(), seleniumBrowser);104 Assert.assertTrue(action instanceof SetInputAction);105 Assert.assertEquals(((SetInputAction)action).getValue(), "Hello");106 Assert.assertEquals(((SetInputAction)action).getProperty(), "id");107 Assert.assertEquals(((SetInputAction)action).getPropertyValue(), "foo");108 }109 @Test110 public void testCheckInput() {111 steps.setBrowser("seleniumBrowser");112 steps.checkInput("uncheck","id", "foo");113 Assert.assertEquals(designer.getTestCase().getActionCount(), 1L);114 Assert.assertTrue(((DelegatingTestAction) designer.getTestCase().getTestAction(0)).getDelegate() instanceof SeleniumAction);115 SeleniumAction action = (SeleniumAction) ((DelegatingTestAction) designer.getTestCase().getTestAction(0)).getDelegate();116 Assert.assertEquals(action.getBrowser(), seleniumBrowser);117 Assert.assertTrue(action instanceof CheckInputAction);118 Assert.assertEquals(((CheckInputAction)action).isChecked(), false);119 Assert.assertEquals(((CheckInputAction)action).getProperty(), "id");120 Assert.assertEquals(((CheckInputAction)action).getPropertyValue(), "foo");121 }122 @Test123 public void testShouldDisplay() {124 steps.setBrowser("seleniumBrowser");125 steps.should_display("name", "foo");126 Assert.assertEquals(designer.getTestCase().getActionCount(), 1L);127 Assert.assertTrue(((DelegatingTestAction) designer.getTestCase().getTestAction(0)).getDelegate() instanceof SeleniumAction);128 SeleniumAction action = (SeleniumAction) ((DelegatingTestAction) designer.getTestCase().getTestAction(0)).getDelegate();129 Assert.assertEquals(action.getBrowser(), seleniumBrowser);130 Assert.assertTrue(action instanceof FindElementAction);131 Assert.assertEquals(((FindElementAction)action).getProperty(), "name");132 Assert.assertEquals(((FindElementAction)action).getPropertyValue(), "foo");133 }134 @Test135 public void testDefaultBrowserInitialization() {136 Assert.assertNull(steps.browser);137 steps.before(Mockito.mock(Scenario.class));138 Assert.assertNotNull(steps.browser);139 }140 @Test141 public void testBrowserInitialization() {142 Assert.assertNull(steps.browser);143 steps.setBrowser("seleniumBrowser");144 steps.before(Mockito.mock(Scenario.class));145 Assert.assertNotNull(steps.browser);146 }147}...
page
Using AI Code Generation
1import com.consol.citrus.cucumber.step.designer.selenium.SeleniumSteps2import com.consol.citrus.dsl.design.TestDesigner3import com.consol.citrus.dsl.design.TestDesignerBeforeTestSupport4import cucumber.api.java.en.Given5import cucumber.api.java.en.Then6import cucumber.api.java.en.When7import org.springframework.beans.factory.annotation.Autowired8import org.springframework.test.context.ContextConfiguration9@ContextConfiguration(classes = [SeleniumSteps::class])10class SeleniumStepsTest : TestDesignerBeforeTestSupport() {11 override fun configure(designer: TestDesigner) {12 designer.apply {13 }14 }15}16import com.consol.citrus.cucumber.step.designer.selenium.SeleniumSteps17import com.consol.citrus.dsl.design.TestDesigner18import com.consol.citrus.dsl.design.TestDesignerBeforeTestSupport19import cucumber.api.java.en.Given20import cucumber.api.java.en.Then21import cucumber.api.java.en.When22import org.springframework.beans.factory.annotation.Autowired23import org.springframework.test.context.ContextConfiguration24@ContextConfiguration(classes = [SeleniumSteps::class])25class SeleniumStepsTest : TestDesignerBeforeTestSupport() {26 override fun configure(designer: TestDesigner) {27 designer.apply {28 }29 }30}31import com.consol.citrus.cucumber.step.designer.selenium.SeleniumSteps32import com.consol.citrus.dsl.design.TestDesigner33import com.consol.citrus.dsl.design.TestDesignerBeforeTestSupport34import cucumber.api.java.en.Given35import cucumber.api.java.en.Then36import cucumber.api.java.en.When37import org.springframework.beans.factory.annotation.Autowired38import org.springframework.test.context.ContextConfiguration39@ContextConfiguration(classes = [SeleniumSteps::class])
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!!