Best Citrus code snippet using com.consol.citrus.cucumber.step.designer.selenium.SeleniumSteps.pages
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}...
pages
Using AI Code Generation
1@Given("I open the test page")2public void iOpenTheTestPage() {3 selenium.open("file:src/test/resources/test.html");4}5@Then("I should see the test page")6public void iShouldSeeTheTestPage() {7 selenium.verifyPageLoaded();8}9@When("I click on the link")10public void iClickOnTheLink() {11 selenium.click("link=Test Link");12}13@Then("I should see the link clicked")14public void iShouldSeeTheLinkClicked() {15 selenium.verifyTextPresent("Test Link Clicked");16}17@When("I click on the button")18public void iClickOnTheButton() {19 selenium.click("button=Test Button");20}21@Then("I should see the button clicked")22public void iShouldSeeTheButtonClicked() {23 selenium.verifyTextPresent("Test Button Clicked");24}25@When("I click on the input")26public void iClickOnTheInput() {27 selenium.click("input=Test Input");28}29@Then("I should see the input clicked")30public void iShouldSeeTheInputClicked() {31 selenium.verifyTextPresent("Test Input Clicked");32}33@When("I click on the image")34public void iClickOnTheImage() {35 selenium.click("image=Test Image");36}37@Then("I should see the image clicked")38public void iShouldSeeTheImageClicked() {39 selenium.verifyTextPresent("Test Image Clicked");40}41@When("I click on the div")42public void iClickOnTheDiv() {43 selenium.click("div=Test Div");44}45@Then("I should see the div clicked")46public void iShouldSeeTheDivClicked() {47 selenium.verifyTextPresent("Test Div Clicked");48}49@When("I click on the span")50public void iClickOnTheSpan() {51 selenium.click("span=Test Span");52}53@Then("I should see the span clicked")54public void iShouldSeeTheSpanClicked() {55 selenium.verifyTextPresent("Test Span Clicked");56}57@When("I click on the paragraph")58public void iClickOnTheParagraph() {59 selenium.click("p=Test Paragraph");60}61@Then("I should see the paragraph clicked")62public void iShouldSeeTheParagraphClicked() {63 selenium.verifyTextPresent("Test Paragraph Clicked");64}65@When("I click on the table")66public void iClickOnTheTable() {
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!!