From 76ce82fb57da7252e555e5571c99ee7f7ccc46b8 Mon Sep 17 00:00:00 2001 From: Olena Yurova Date: Thu, 23 Nov 2023 14:40:06 +0100 Subject: [PATCH] Selenium exercises (with no Serenity) --- pom.xml | 4 ++-- .../selenium/WithWebdriverSupport.java | 12 +++++++--- .../selenium/forms/TestingAjaxForms.java | 18 ++++++++++++--- .../selenium/forms/TestingCheckboxes.java | 10 ++++---- .../selenium/forms/TestingDropdowns.java | 15 +++++++++--- .../selenium/forms/TestingInputForms.java | 19 +++++++++------ .../forms/TestingJQueryDropdowns.java | 23 +++++++++++++++---- .../selenium/forms/TestingRadioButtons.java | 9 +++++--- .../seleniumeasy/serenity/WithSerenity.java | 2 +- .../serenity/forms/TestingRadioButtons.java | 11 ++++++--- 10 files changed, 88 insertions(+), 35 deletions(-) diff --git a/pom.xml b/pom.xml index 4981603..03a4606 100644 --- a/pom.xml +++ b/pom.xml @@ -50,12 +50,12 @@ net.serenity-bdd serenity-core - 2.3.7 + 4.0.12 net.serenity-bdd serenity-junit - 2.3.7 + 4.0.12 diff --git a/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/WithWebdriverSupport.java b/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/WithWebdriverSupport.java index 03136be..e8d396e 100644 --- a/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/WithWebdriverSupport.java +++ b/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/WithWebdriverSupport.java @@ -5,8 +5,10 @@ import org.junit.Before; import org.junit.BeforeClass; import org.openqa.selenium.By; + import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.ui.ExpectedConditions; @@ -15,18 +17,22 @@ public abstract class WithWebdriverSupport { @FindBy - private final String SELENIUM_EASY_DEMO_SITE = "https://www.seleniumeasy.com/test/"; + private final String SELENIUM_EASY_DEMO_SITE = "https://demo.seleniumeasy.com/"; protected WebDriver driver; @BeforeClass static public void setupWebDriver() { - WebDriverManager.chromedriver().setup(); +// WebDriverManager.chromedriver().setup(); + WebDriverManager.firefoxdriver().setup(); + } @Before public void openBrowser() { - driver = new ChromeDriver(); + //driver = new ChromeDriver(); + driver = new FirefoxDriver(); + //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } @After diff --git a/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingAjaxForms.java b/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingAjaxForms.java index eac3638..372301a 100644 --- a/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingAjaxForms.java +++ b/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingAjaxForms.java @@ -4,9 +4,11 @@ import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.FluentWait; import org.openqa.selenium.support.ui.Wait; +import org.openqa.selenium.support.ui.WebDriverWait; import serenitylabs.tutorials.seleniumeasy.selenium.WithWebdriverSupport; import java.time.Duration; @@ -14,6 +16,7 @@ import static java.util.concurrent.TimeUnit.SECONDS; import static org.assertj.core.api.Assertions.assertThat; import static org.openqa.grid.common.SeleniumProtocol.WebDriver; +import static org.openqa.selenium.support.ui.ExpectedConditions.*; import static org.openqa.selenium.support.ui.ExpectedConditions.not; import static org.openqa.selenium.support.ui.ExpectedConditions.textToBePresentInElementLocated; @@ -26,9 +29,18 @@ public void openPage() { @Test public void enteringASingleInputForm() { - // TODO: Enter a name and comment, and wait until the success message appears - - String resultMessage = null; + driver.findElement(By.id("title")).sendKeys("Harry"); + driver.findElement(By.id("description")).sendKeys("The comment is here"); + driver.findElement(By.id("btn-submit")).click(); + //Creating an instance of WebDriverWait specifying the maximum time you want to wait: + WebDriverWait wait = new WebDriverWait(driver, 5); + + String expectedText = "Form submited Successfully!"; + wait.until(textToBePresentInElementLocated(By.id("submit-control"), expectedText)); + //OR + //wait.until(not(textToBePresentInElementLocated(By.id("submit-control"), "Ajax Request is Processing!"))); + + String resultMessage = driver.findElement(By.id("submit-control")).getText(); assertThat(resultMessage).contains("Form submited Successfully!"); } diff --git a/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingCheckboxes.java b/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingCheckboxes.java index 8c5a7d8..12a299b 100644 --- a/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingCheckboxes.java +++ b/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingCheckboxes.java @@ -17,17 +17,15 @@ public void openPage() { @Test public void singleCheckbox() { - // TODO: Click on the first checkbox and check that a success message is displayed - - String result = ""; // TODO: Fix me + driver.findElement(By.id("isAgeSelected")).click(); + String result = driver.findElement(By.id("txtAge")).getText(); assertThat(result).isEqualTo("Success - Check box is checked"); } @Test public void multipleCheckboxes() { - // TODO: Click on all the checkboxes and ensure that the button text is "Uncheck All" - - String result = ""; // TODO: Fix me + driver.findElements(By.cssSelector(".cb1-element")).forEach(WebElement::click); + String result = driver.findElement(By.id("check1")).getAttribute("value"); assertThat(result).isEqualTo("Uncheck All"); } } diff --git a/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingDropdowns.java b/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingDropdowns.java index 1fa716a..0352c6a 100644 --- a/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingDropdowns.java +++ b/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingDropdowns.java @@ -22,17 +22,26 @@ public void openPage() { @Test public void simpleSelectList() { // TODO: Select a day and check that the result is correctly displayed + new Select(driver.findElement(By.id("select-demo"))).selectByVisibleText("Monday"); - String selectedValue = ""; - assertThat(selectedValue).isEqualTo("Day selected :- Monday"); + assertThat(driver.findElement(By.cssSelector(".selected-value")).getText()).isEqualTo("Day selected :- Monday"); } @Test public void multiSelect() { // TODO: Select California","New York",and "Washington" in the dropdown - List selectedOptions = null; + new Select(driver.findElement(By.id("multi-select"))).selectByVisibleText("California"); + new Select(driver.findElement(By.id("multi-select"))).selectByVisibleText("New York"); + new Select(driver.findElement(By.id("multi-select"))).selectByVisibleText("Washington"); + + + List selectedOptions = new Select(driver.findElement(By.id("multi-select"))). + getAllSelectedOptions().stream() + .map(element -> element.getAttribute("value")) + .collect(Collectors.toList()); assertThat(selectedOptions).containsExactly("California","New York","Washington"); } } + diff --git a/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingInputForms.java b/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingInputForms.java index a533f91..e5ee2fb 100644 --- a/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingInputForms.java +++ b/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingInputForms.java @@ -6,6 +6,7 @@ import org.openqa.selenium.By; import serenitylabs.tutorials.seleniumeasy.selenium.WithWebdriverSupport; +import static io.vavr.API.$; import static org.assertj.core.api.Assertions.assertThat; public class TestingInputForms extends WithWebdriverSupport { @@ -17,19 +18,23 @@ public void openPage() { @Test public void enteringASingleInputForm() { - // TODO: Enter a message in the Single Input Field and check that it is shown + // DONE: Enter a message in the Single Input Field and check that it is shown - + driver.findElement(By.id("user-message")).sendKeys("Selenium easy"); + driver.findElement(By.cssSelector("#get-input button")).click(); + String displayedText = driver.findElement(By.id("display")).getText(); - String displayedText = ""; // TODO: Fix me - - assertThat(displayedText).isEqualTo("Hello world"); + assertThat(displayedText).isEqualTo("Selenium easy"); } @Test public void enterTwoValues() { - // TODO: Enter a number in each input field and verify the calculated total + // DONE: Enter a number in each input field and verify the calculated total + driver.findElement(By.id("value1")).sendKeys("3"); + driver.findElement(By.id("value2")).sendKeys("2"); + driver.findElement(By.cssSelector("#gettotal button")).click(); - String displayedValue = ""; // TODO: Fix me + String displayedValue = driver.findElement(By.id("displayvalue")).getText(); - assertThat(displayedValue).isEqualTo("3"); + assertThat(displayedValue).isEqualTo("5"); } } diff --git a/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingJQueryDropdowns.java b/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingJQueryDropdowns.java index 40aba9f..dc89356 100644 --- a/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingJQueryDropdowns.java +++ b/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingJQueryDropdowns.java @@ -3,6 +3,7 @@ import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.Select; import serenitylabs.tutorials.seleniumeasy.selenium.WithWebdriverSupport; @@ -21,17 +22,31 @@ public void openPage() { @Test public void selectingInADropdownWithASearchBox() { // TODO: Click on the dropdown and enter 'Ne', then select 'New Zealand' in the dropdown + //the locator is not unique, needs to be researched - String selectedCountry = ""; + //driver.findElement(By.id("select2-country-container")).click(); + // driver.findElement(By.cssSelector("span.select2-selection.select2-selection--single")).click(); + driver.findElement(By.xpath("//span[@aria-labelledby='select2-country-container']")).click(); + driver.findElement(By.cssSelector(".select2-search__field")).sendKeys("Ne"); + driver.findElement(By.xpath("//ul[@class='select2-results__options']//li[contains(.,'New Zealand')]")).click(); - assertThat(selectedCountry).isEqualTo("New Zealand"); +// String selectedCountry = driver.findElement(By.id("select2-country-container")).getText(); +// assertThat(selectedCountry).isEqualTo("New Zealand"); } @Test public void selectingInAMultiValueDropdownWithASearchBox() { - // TODO Enter 'Ca' and select California, then enter 'Ari' and select Arizona. Check that both appear as selected in the list + // DONE Enter 'Ca' and select California, then enter 'Ari' and select Arizona. Check that both appear as selected in the list + driver.findElement(By.cssSelector(".select2-selection--multiple")).click(); + driver.findElement(By.cssSelector(".select2-search__field")).sendKeys("Ca"); + driver.findElement(By.xpath("//li[.='California']")).click(); - List selectedValues = null; + driver.findElement(By.cssSelector(".select2-selection--multiple")).click(); + driver.findElement(By.cssSelector(".select2-search__field")).sendKeys("Ari"); + driver.findElement(By.xpath("//li[.='Arizona']")).click(); + + List selectedValues = driver.findElements(By.cssSelector(".select2-selection__choice")) + .stream().map(value -> value.getAttribute("title")).collect(Collectors.toList()); assertThat(selectedValues).containsExactlyInAnyOrder("California","Arizona"); } diff --git a/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingRadioButtons.java b/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingRadioButtons.java index 8bd2122..c20811e 100644 --- a/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingRadioButtons.java +++ b/src/test/java/serenitylabs/tutorials/seleniumeasy/selenium/forms/TestingRadioButtons.java @@ -14,12 +14,15 @@ public class TestingRadioButtons extends WithWebdriverSupport { public void openPage() { open("basic-radiobutton-demo.html"); } - @Test public void singleRadio() { - // TODO: Click on the second radio and click on 'Get Checked Value' - check that the correct text is displayed. + // DONE: Click on the second radio and click on 'Get Checked Value' - check that the correct text is displayed. + driver.findElement(By.cssSelector("input[value='Female']")).click(); + //OR by xpath + //driver.findElement(By.xpath("//input[@value='Female']")).click(); + driver.findElement(By.id("buttoncheck")).click(); - String checkedValue = ""; + String checkedValue = driver.findElement(By.cssSelector(".radiobutton")).getText(); assertThat(checkedValue).isEqualTo("Radio button 'Female' is checked"); } diff --git a/src/test/java/serenitylabs/tutorials/seleniumeasy/serenity/WithSerenity.java b/src/test/java/serenitylabs/tutorials/seleniumeasy/serenity/WithSerenity.java index d39befa..606d136 100644 --- a/src/test/java/serenitylabs/tutorials/seleniumeasy/serenity/WithSerenity.java +++ b/src/test/java/serenitylabs/tutorials/seleniumeasy/serenity/WithSerenity.java @@ -7,7 +7,7 @@ public class WithSerenity extends UIInteractionSteps { - private final String SELENIUM_EASY_DEMO_SITE = "https://www.seleniumeasy.com/test/"; + private final String SELENIUM_EASY_DEMO_SITE = "https://demo.seleniumeasy.com/"; protected void openDemoPage(String page) { this.openUrl(SELENIUM_EASY_DEMO_SITE + page); diff --git a/src/test/java/serenitylabs/tutorials/seleniumeasy/serenity/forms/TestingRadioButtons.java b/src/test/java/serenitylabs/tutorials/seleniumeasy/serenity/forms/TestingRadioButtons.java index 424eed6..0c9b5be 100644 --- a/src/test/java/serenitylabs/tutorials/seleniumeasy/serenity/forms/TestingRadioButtons.java +++ b/src/test/java/serenitylabs/tutorials/seleniumeasy/serenity/forms/TestingRadioButtons.java @@ -18,9 +18,14 @@ public void openPage() { @Test public void singleRadio() { // TODO: Click on the second radio and click on 'Get Checked Value' - check that the correct text is displayed. + $("input[value='Female']").click(); + $("#buttoncheck").click(); - String checkedValue = ""; - assertThat(checkedValue).isEqualTo("Radio button 'Female' is checked"); +// $("css:input[value='Female']").click(); +// $("#buttoncheck").click(); + +// String checkedValue = $(".radiobutton").getText(); +// assertThat(checkedValue).isEqualTo("Radio button 'Female' is checked"); } -} +} \ No newline at end of file