diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/pom.xml b/pom.xml index c6dac1a..957fbcc 100644 --- a/pom.xml +++ b/pom.xml @@ -12,67 +12,26 @@ UTF-8 - 1.7 - 1.7 + 1.8 + 1.8 org.junit.jupiter junit-jupiter-api - 5.3.2 + 5.4.0 test org.seleniumhq.selenium selenium-java - 3.10.0 + 4.0.0-rc-3 org.apache.commons commons-lang3 3.6 - - commons-io - commons-io - 2.5 - - - - - - - maven-clean-plugin - 3.0.0 - - - - maven-resources-plugin - 3.0.2 - - - maven-compiler-plugin - 3.7.0 - - - maven-surefire-plugin - 2.20.1 - - - maven-jar-plugin - 3.0.2 - - - maven-install-plugin - 2.5.2 - - - maven-deploy-plugin - 2.8.2 - - - - diff --git a/src/main/java/browser/BrowserGetter.java b/src/main/java/browser/BrowserGetter.java index 3a12832..9a7e955 100644 --- a/src/main/java/browser/BrowserGetter.java +++ b/src/main/java/browser/BrowserGetter.java @@ -1,7 +1,9 @@ package browser; +import org.openqa.selenium.Dimension; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.firefox.FirefoxDriver; import static java.lang.System.setProperty; import static org.apache.commons.lang3.SystemUtils.*; @@ -12,9 +14,15 @@ public class BrowserGetter { * if it is not one of the predefined ones, just throw exception * only create a webDriver instance for a known operating system */ - public WebDriver getChromeDriver() { + public WebDriver getWinChromeDriver() { + setProperty("webdriver.chrome.driver", "src/test/resources/browserBinaries/chromedriver.exe"); + WebDriver driver = new ChromeDriver(); + driver.manage().window().maximize(); + return driver; + } + public WebDriver getChromeDriver() { if (!IS_OS_WINDOWS && !IS_OS_LINUX && !IS_OS_MAC) { throw new RuntimeException("Could not initialize browser due to unknown operating system!"); } @@ -32,4 +40,56 @@ public WebDriver getChromeDriver() { driver.manage().window().maximize(); return driver; } + + public WebDriver getFirefoxDriver() { + if (!IS_OS_WINDOWS && !IS_OS_LINUX && !IS_OS_MAC) { + throw new RuntimeException("Could not initialize browser due to unknown operating system!"); + } + if (IS_OS_WINDOWS) { + setProperty("webdriver.gecko.driver", "src/test/resources/browserBinaries/geckodriver.exe"); + } + if (IS_OS_LINUX) { + setProperty("webdriver.gecko.driver", "src/test/resources/browserBinaries/geckodriver"); + } + if (IS_OS_MAC) { + setProperty("webdriver.gecko.driver", "src/test/resources/browserBinaries/geckodriverMac"); + } + + WebDriver driver = new FirefoxDriver(); + driver.manage().window().maximize(); + return driver; + } + + public WebDriver getDriver() { + switch (System.getProperty("browser").toLowerCase()) { + case "chrome" : + System.out.println("Chrome was chosen!"); + return getChromeDriver(); + case "firefox" : + System.out.println("Firefox was chosen!"); + return getFirefoxDriver(); + default: + throw new RuntimeException("Unsupported browser! Will not start any browser!"); + } + } + + public WebDriver getChromeDriverCustomSize(int width, int height) { + if (!IS_OS_WINDOWS && !IS_OS_LINUX && !IS_OS_MAC) { + throw new RuntimeException("Could not initialize browser due to unknown operating system!"); + } + if (IS_OS_WINDOWS) { + setProperty("webdriver.chrome.driver", "src/test/resources/browserBinaries/chromedriver.exe"); + } + if (IS_OS_LINUX) { + setProperty("webdriver.chrome.driver", "src/test/resources/browserBinaries/chromedriver"); + } + if (IS_OS_MAC) { + setProperty("webdriver.chrome.driver", "src/test/resources/browserBinaries/chromedriverMac"); + } + + WebDriver driver = new ChromeDriver(); + driver.manage().window().setSize(new Dimension(width, height)); + return driver; + } + } diff --git a/src/main/java/objects/Article.java b/src/main/java/objects/Article.java new file mode 100644 index 0000000..aa84b32 --- /dev/null +++ b/src/main/java/objects/Article.java @@ -0,0 +1,48 @@ +package objects; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import java.util.Objects; + +public class Article { + private Image img; + private String tag; + private Link link; + + public Article(Image img, String tag, Link link) { + this.img = img; + this.tag = tag; + this.link = link; + } + + public Article(WebElement element) { + this.img = new Image(element.findElement(By.cssSelector("img"))); + this.tag = element.findElement(By.cssSelector("span")).getText(); + this.link = new Link(element.findElement(By.cssSelector("a"))); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Article article = (Article) o; + return Objects.equals(img, article.img) && + Objects.equals(tag, article.tag) && + Objects.equals(link, article.link); + } + + @Override + public int hashCode() { + return Objects.hash(img, tag, link); + } + + @Override + public String toString() { + return "Article{" + + "img=" + img + + ", tag='" + tag + '\'' + + ", link=" + link + + '}'; + } +} diff --git a/src/main/java/objects/Image.java b/src/main/java/objects/Image.java new file mode 100644 index 0000000..4658441 --- /dev/null +++ b/src/main/java/objects/Image.java @@ -0,0 +1,47 @@ +package objects; + +import org.openqa.selenium.WebElement; + +import java.util.Objects; + +public class Image { + private String src; + private String height; + private String width; + + public Image(String src, String height, String width) { + this.src = src; + this.height = height; + this.width = width; + } + + public Image(WebElement element) { + this.src = element.getAttribute("src"); + this.height = element.getAttribute("height"); + this.width = element.getAttribute("width"); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Image image = (Image) o; + return Objects.equals(src, image.src) && + Objects.equals(height, image.height) && + Objects.equals(width, image.width); + } + + @Override + public int hashCode() { + return Objects.hash(src, height, width); + } + + @Override + public String toString() { + return "Image{" + + "src='" + src + '\'' + + ", height='" + height + '\'' + + ", width='" + width + '\'' + + '}'; + } +} diff --git a/src/main/java/objects/Link.java b/src/main/java/objects/Link.java new file mode 100644 index 0000000..ea61241 --- /dev/null +++ b/src/main/java/objects/Link.java @@ -0,0 +1,42 @@ +package objects; + +import org.openqa.selenium.WebElement; + +import java.util.Objects; + +public class Link { + private String url; + private String label; + + public Link(String url, String label) { + this.url = url; + this.label = label; + } + + public Link(WebElement element) { + this.url = element.getAttribute("href"); + this.label = element.getText(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Link link = (Link) o; + return Objects.equals(url, link.url) && + Objects.equals(label, link.label); + } + + @Override + public int hashCode() { + return Objects.hash(url, label); + } + + @Override + public String toString() { + return "Link{" + + "url='" + url + '\'' + + ", label='" + label + '\'' + + '}'; + } +} diff --git a/src/main/java/pages/InteractionsPage.java b/src/main/java/pages/InteractionsPage.java new file mode 100644 index 0000000..f1ffa70 --- /dev/null +++ b/src/main/java/pages/InteractionsPage.java @@ -0,0 +1,30 @@ +package pages; + +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; + +import java.util.List; + +public class InteractionsPage { + @FindBy(css = "[name='checkboxToClick']") public WebElement checkbox; + @FindBy(css = "#radioButtonToClick") public WebElement radioButton; + @FindBy(css = "#linkToClick") public WebElement link; + + @FindBy(css = "[type='text']") public WebElement textInput; + @FindBy(css = "textarea") public WebElement textArea; + @FindBy(css = "[maxlength='100']") public WebElement predefinedTextArea; + + @FindBy(css = "[src*='42514697560_1740d1993f_n.jpg']") public WebElement img; + @FindBy(css = ".w3-purple") public WebElement disabledButton; + + @FindBy(css = "#getTextOuterDiv") public WebElement div; + @FindBy(css = "#getTextInnerDiv h5") public WebElement h5; + + @FindBy(css = "#getCssValueDiv") public WebElement container; + @FindBy(css = "#getCssValueDiv h2") public WebElement h2; + + @FindBy(css = "[name='coffee']") public WebElement coffeeSelectWebElement; + @FindBy(css = "[name='refreshment']") public WebElement refreshmentSelectWebElement; + + @FindBy(css = "h1") public WebElement h1; +} diff --git a/src/main/java/pages/SeOOPage.java b/src/main/java/pages/SeOOPage.java new file mode 100644 index 0000000..d398d26 --- /dev/null +++ b/src/main/java/pages/SeOOPage.java @@ -0,0 +1,11 @@ +package pages; + +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; + +import java.util.List; + +public class SeOOPage { + @FindBy(css = "article") public + List elements; +} diff --git a/src/main/java/pages/TabsPage.java b/src/main/java/pages/TabsPage.java new file mode 100644 index 0000000..c0dd8d3 --- /dev/null +++ b/src/main/java/pages/TabsPage.java @@ -0,0 +1,10 @@ +package pages; + +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; + +public class TabsPage { + @FindBy(css = "[href*='secondpage']") public WebElement linkToSecondPage; + @FindBy(css = "[href*='thirdpage']") public WebElement linkToThirdPage; + @FindBy(css = "h1") public WebElement h1; +} diff --git a/src/main/java/tutorialSolution/pages/BasicPage.java b/src/main/java/tutorialsolution/pages/BasicPage.java similarity index 71% rename from src/main/java/tutorialSolution/pages/BasicPage.java rename to src/main/java/tutorialsolution/pages/BasicPage.java index ddbb1bb..ced5d76 100644 --- a/src/main/java/tutorialSolution/pages/BasicPage.java +++ b/src/main/java/tutorialsolution/pages/BasicPage.java @@ -1,4 +1,4 @@ -package tutorialSolution.pages; +package tutorialsolution.pages; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; @@ -28,10 +28,10 @@ public class BasicPage { @FindBy(css = "div") public List divElements; //image whose width attribute equals 189 - @FindBy(css = "img[width='189']") + @FindBy(css = "[width='189'][height='125']") public WebElement imageWithSpecifiedWidth; //image whose sources (src) attribute contains 1878 - @FindBy(css = "img[src*='1878']") + @FindBy(css = "[src*='1878']") public WebElement imageWithSrcContains; //relativity: first button from the module with id relativeModule @FindBy(css = "#relativeModule button") @@ -51,6 +51,8 @@ public class BasicPage { public WebElement level2SiblingsWithDifferentTags; @FindBy(css = "#level2SiblingsSameTag h3") public List level2SiblingsSameTag; + @FindBy(css = "#level2SiblingsSameTagAndClass .someClass") + public WebElement level2SiblingsSameTagAndAlphaHasClass; @FindBy(css = "select[name='BRAVO'] option") public List dropdownOptions; @FindBy(css = "#BRAVOUL li") @@ -61,7 +63,19 @@ public class BasicPage { public WebElement level3; @FindBy(css = "#level3AEqualsB div div") public WebElement level3AlphaEqualsBravo; + @FindBy(css = "#level3AEqualsBAHasClass .w3-red") + public WebElement level3AlphaEqualsBravoAlphaHasClass; @FindBy(css = "#level3BravoIdenticalSiblings h3") public List level3BravoIdenticalSiblings; @FindBy(css = "#level3AllIdenticalSiblings div div") public List level3AllIdentical; @FindBy(css = "#level3OneBravoSeveralAlphas h3") public List level3OneBravoSeveralAlphas; + + //iframes page + //button found inside the frame identified by id + @FindBy(css = "#btnForFrameWithId") public WebElement buttonForFrameWithId; + //button found inside the frame identified by index + @FindBy(css = "#btnForFrameWithIndex") public WebElement buttonForFrameWithIndex; + //button found inside the frame identified as a WebElement + @FindBy(css = "#btnForFrameAsWebElement") public WebElement buttonForFrameWithName; + //selector for identifying frame as WebElement (when frame has no id, name and will not be identified by index) + @FindBy(css = "[src*='FrameAsWebElement']") public WebElement frameAsWebElement; } diff --git a/src/main/java/tutorialsolution/pages/UserPromptsPage.java b/src/main/java/tutorialsolution/pages/UserPromptsPage.java new file mode 100644 index 0000000..5c7de04 --- /dev/null +++ b/src/main/java/tutorialsolution/pages/UserPromptsPage.java @@ -0,0 +1,11 @@ +package tutorialsolution.pages; + +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; + +public class UserPromptsPage { + @FindBy(css = "#alertButton") public WebElement alertButton; + @FindBy(css = "#confirmButton") public WebElement confirmButton; + @FindBy(css = "#promptButton") public WebElement promptButton; + @FindBy(css = "#userClicked") public WebElement userClicked; +} diff --git a/src/main/java/tutorialsolution/pages/WebElementInteractionPage.java b/src/main/java/tutorialsolution/pages/WebElementInteractionPage.java new file mode 100644 index 0000000..93b2b2a --- /dev/null +++ b/src/main/java/tutorialsolution/pages/WebElementInteractionPage.java @@ -0,0 +1,47 @@ +package tutorialsolution.pages; + +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; + +public class WebElementInteractionPage { + //WebElement with ID + @FindBy(css = "#idModuleImage") + public WebElement elementWithId; + + //inputs section + @FindBy(css = "[name='checkedCheckbox']") public WebElement checkedCheckbox; + @FindBy(css = "[name='uncheckedCheckbox']") public WebElement uncheckedCheckbox; + @FindBy(css = "#myRadio") public WebElement radioButton; + + //elements for clicking + @FindBy(css = "[name='checkboxToClick']") public WebElement checkboxToClick; + @FindBy(css = "#radioButtonToClick") public WebElement radioButtonToClick; + @FindBy(css = "#buttonToClick") public WebElement buttonToClick; + @FindBy(css = "#linkToClick") public WebElement linkToClick; + + //elements for typing + @FindBy(css = "[type='text']") public WebElement textInput; + @FindBy(css = "textarea") public WebElement textarea; + + //disabled button + @FindBy(css = "input[type='button']") public WebElement disabledButton; + + //elements for getText + @FindBy(css = "#getTextOuterDiv") public WebElement getTextOuterDiv; + @FindBy(css = "#getTextInnerDiv") public WebElement getTextInnerDiv; + @FindBy(css = "#getTextInnerDiv h5") public WebElement getTexth5; + @FindBy(css = "#getTextInnerDiv img") public WebElement getTextImg; + @FindBy(css = "#getTextInnerDiv a") public WebElement getTextLink; + @FindBy(css = "#getTextInnerDiv button") public WebElement getTextButton; + @FindBy(css = "#getTextInnerDiv select option") public WebElement getTextDropdownFirstOption; + @FindBy(css = "[maxlength='100']") public WebElement getTextTextarea; + + //dropdowns + @FindBy(css = "[name='coffee']") public WebElement coffeeSelect; + @FindBy(css = "[name='tea']") public WebElement teaSelect; + @FindBy(css = "[name='refreshment']") public WebElement refreshmentSelect; + + //for getting the CSS attributes + @FindBy(css = "h2") public WebElement h2Element; + @FindBy(css = "#getCssValueDiv") public WebElement h2DivElement; +} diff --git a/src/main/java/tutorialsolution/pages/WindowsPage.java b/src/main/java/tutorialsolution/pages/WindowsPage.java new file mode 100644 index 0000000..a4993b2 --- /dev/null +++ b/src/main/java/tutorialsolution/pages/WindowsPage.java @@ -0,0 +1,10 @@ +package tutorialsolution.pages; + +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; + +public class WindowsPage { + @FindBy(css = "[href*='secondpage']") public WebElement linkToSecondPage; + @FindBy(css = "[href*='thirdpage']") public WebElement linkToThirdPage; + @FindBy(css = "h1") public WebElement h1Element; +} diff --git a/src/main/java/waiter/Waiter.java b/src/main/java/waiter/Waiter.java new file mode 100644 index 0000000..dfc8641 --- /dev/null +++ b/src/main/java/waiter/Waiter.java @@ -0,0 +1,982 @@ +package waiter; + +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.ExpectedCondition; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.Wait; +import org.openqa.selenium.support.ui.WebDriverWait; + +/** + * A class that holds only Selenium wait methods + */ +public class Waiter { + //hardcoded timeout value; up to how much to wait for a condition to take place + private final int TIMEOUT = 30; + + //GET METHODS + + /** + * Open a specified url and wait for the page to load completely. + * Should replace driver.get(). + * Will wait for the TIMEOUT constant value amount of seconds for the page to load. + * + * @param url - the url to open in the browser + * @param driver - the driver instance + */ + public void get(String url, WebDriver driver) { + get(url, driver, TIMEOUT); + } + + /** + * Open a specified url and wait for the page to load completely. + * Should replace driver.get(). + * Will wait for the specified number of seconds. + * + * @param url - the url to open in the browser + * @param driver - the driver instance + * @param specifiedTimeout - number of seconds to wait for + */ + public void get(String url, WebDriver driver, int specifiedTimeout) { + driver.get(url); + waitForPageLoadComplete(driver, specifiedTimeout); + } + + /** + * Method that opens a url and waits for page load complete and for a specified WebElement to be displayed. + * Uses Selenium's isDisplayed() method to check for element. + * Wait for up to TIMEOUT. + * + * @param url - url of the page that needs to open + * @param element - the element to wait for + * @param driver - the WebDriver instance + */ + public void getAndWaitForElementToBeDisplayed(String url, WebElement element, WebDriver driver) { + getAndWaitForElementToBeDisplayed(url, element, driver, TIMEOUT); + } + + /** + * Method that opens a url and waits for page load complete and for a specified WebElement to be displayed. + * Uses Selenium's isDisplayed() method to check for element. + * Wait for up to the specified amount of seconds. + * + * @param url - url of the page that needs to open + * @param element - the element to wait for + * @param driver - the WebDriver instance + * @param specifiedTimeout - number of seconds to wait for + */ + public void getAndWaitForElementToBeDisplayed(String url, WebElement element, WebDriver driver, int + specifiedTimeout) { + get(url, driver, specifiedTimeout); + waitForElementToBeDisplayed(element, driver, specifiedTimeout); + } + + /** + * Method that opens a URL in the browser and waits for another URL to be loaded. + * Also waits for page to load completely after expected URL is loaded in the browser. + * Useful when a URL you want to open performs a redirect to another page. + * Wait for TIMEOUT number of seconds. + * + * @param urlToGet - the URL to open initially + * @param urlToWaitFor - the URL you expect to be redirected to + * @param driver - the WebDriver instance + */ + public void getUrlAndWaitForUrl(String urlToGet, String urlToWaitFor, WebDriver driver) { + getUrlAndWaitForUrl(urlToGet, urlToWaitFor, driver, TIMEOUT); + } + + /** + * Method that opens a URL in the browser and waits for another URL to be loaded. + * Also waits for page to load completely after expected URL is loaded in the browser. + * Useful when a URL you want to open performs a redirect to another page. + * Wait for a specifiedTimeout number of seconds. + * + * @param urlToGet - the URL to open initially + * @param urlToWaitFor - the URL you expect to be redirected to + * @param driver - the WebDriver instance + * @param specifiedTimeout - number of seconds to wait for + */ + public void getUrlAndWaitForUrl(String urlToGet, String urlToWaitFor, WebDriver driver, int specifiedTimeout) { + driver.get(urlToGet); + waitForUrl(urlToWaitFor, driver, specifiedTimeout); + } + + //PAGE LOAD METHODS + + /** + * Wait for a page to load completely for up to TIMEOUT seconds. + * + * @param driver - the WebDriver instance + */ + public void waitForPageLoadComplete(WebDriver driver) { + waitForPageLoadComplete(driver, TIMEOUT); + } + + /** + * Wait for a page to load completely for the specified number of seconds. + * + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForPageLoadComplete(WebDriver driver, int specifiedTimeout) { + Wait wait = new WebDriverWait(driver, specifiedTimeout); + wait.until(driver1 -> String + .valueOf(((JavascriptExecutor) driver1).executeScript("return document.readyState")) + .equals("complete")); + } + + //ELEMENT WAIT METHODS + + /** + * Method that waits for the Selenium isDisplayed() method to return true. + * Hence waits for an element to be displayed. + * Will wait for up to TIMEOUT seconds. + * + * @param element - the WebElement to be displayed + * @param driver - the WebDriver instance + */ + public void waitForElementToBeDisplayed(WebElement element, WebDriver driver) { + waitForElementToBeDisplayed(element, driver, TIMEOUT); + } + + /** + * Method that waits for the Selenium isDisplayed() method to return true. + * Hence waits for an element to be displayed. + * Will wait for up to the specified amount of seconds. + * + * @param element - the WebElement to be displayed + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForElementToBeDisplayed(WebElement element, WebDriver driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition elementIsDisplayed = arg0 -> element.isDisplayed(); + wait.until(elementIsDisplayed); + } + + public void clickWithExpectedConditions(WebElement element, WebDriver driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + wait.until(ExpectedConditions.elementToBeClickable(element)); + element.click(); + } + + /** + * Try to click on an element during the TIMEOUT number of seconds. + * + * @param element - the element to click on + * @param driver - the WebDriver instance + */ + public void click(WebElement element, WebDriver driver) { + click(element, driver, TIMEOUT); + } + + /** + * Try to click on an element during the specifiedTimeout number of seconds. + * + * @param element - the element to click on + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void click(WebElement element, WebDriver driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition elementIsClickable = arg0 -> { + try { + element.click(); + return true; + } catch (Exception e) { + return false; + } + }; + wait.until(elementIsClickable); + } + + /** + * Method that waits for the text on the element to equal an expected String. + * Compares the value resulted from getText() on the element with the expected String. + * Will wait for up to the TIMEOUT number of seconds for the text on the element to be the expected one. + * + * @param element - the WebElement whose text will be compared to an expected value + * @param expectedString - the expected value of the WebElement's text + * @param driver - the WebDriver instance + */ + public void waitForElementTextEqualsString(WebElement element, String expectedString, WebDriver driver) { + waitForElementTextEqualsString(element, expectedString, driver, TIMEOUT); + } + + /** + * Method that waits for the text on the element to equal an expected String. + * Compares the value resulted from getText() on the element with the expected String. + * Will wait for up to the specifiedTimeout number of seconds for the text on the element to be the expected one. + * + * @param element - the WebElement whose text will be compared to an expected value + * @param expectedString - the expected value of the WebElement's text + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForElementTextEqualsString(WebElement element, String expectedString, WebDriver driver, int + specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition elementTextEqualsString = arg0 -> element.getText().equals(expectedString); + wait.until(elementTextEqualsString); + } + + /** + * Method that waits for the text on the element to equal an expected String but ignoring the case of the two. + * Compares the value resulted from getText() on the element with the expected String, but without taking into + * account the case of the two values. + * Therefore, for example 'tHis' and 'This' will be equal when calling this method. + * Will wait for up to the TIMEOUT number of seconds for the text on the element to be the expected one. + * + * @param element - the WebElement whose text will be compared to an expected value + * @param expectedString - the expected value of the WebElement's text + * @param driver - the WebDriver instance + */ + public void waitForElementTextEqualsString_IgnoreCase(WebElement element, String expectedString, WebDriver driver) { + waitForElementTextEqualsString_IgnoreCase(element, expectedString, driver, TIMEOUT); + } + + /** + * Method that waits for the text on the element to equal an + * expected String but ignoring the case of the two. + * Compares the value resulted from getText() on the element with the expected String, but without taking into + * account the case of the two values. + * Therefore, for example 'tHis' and 'This' will be equal when calling this method. + * Will wait for up to the specifiedTimeout number of seconds for the text on the element to be the expected one. + * + * @param element - the WebElement whose text will be compared to an expected value + * @param expectedString - the expected value of the WebElement's text + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForElementTextEqualsString_IgnoreCase(WebElement element, String expectedString, WebDriver driver, + int + specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition elementTextEqualsStringIgnoreCase = arg0 -> element.getText().equalsIgnoreCase + (expectedString); + wait.until(elementTextEqualsStringIgnoreCase); + } + + /** + * Method that waits for the text on the element (whose + * whitespaces are removed) to equal an expected String (whose whitespaces are also removed). + * Basically, does a getText() on the WebElement, removes all whitespaces from this resulting String, then compares + * this value to another String that contains no whitespaces. + * Whitespaces include: space, new line, tab. + * Having said that, only the non whitespace characters are compared. + * When calling the method, the expectedString can contain whitespaces, as they are removed inside this method. + * Therefore, 'this string here' will equal 'this string here'. + * Will wait for up to the TIMEOUT number of seconds for the text on the element to be the expected one. + * + * @param element - the WebElement whose text will be compared to an expected value after removing the + * whitespaces on this text + * @param expectedString - the expected value of the WebElement's text on which a whitespace removal is also done + * @param driver - the WebDriver instance + */ + public void waitForElementTextEqualsString_IgnoreWhitespaces(WebElement element, String expectedString, WebDriver + driver) { + waitForElementTextEqualsString_IgnoreWhitespaces(element, expectedString, driver, TIMEOUT); + } + + /** + * Method that waits for the text on the element (whose + * whitespaces are removed) to equal an expected String (whose whitespaces are also removed). + * Basically, does a getText() on the WebElement, removes all whitespaces from this resulting String, then compares + * this value to another String that contains no whitespaces. + * Whitespaces include: space, new line, tab. + * When calling the method, the expectedString can contain whitespaces, as they are removed inside this method. + * Therefore, 'this string here' will equal 'this string here'. + * Will wait for up to the specifiedTimeout number of seconds for the text on the element to be the expected one. + * + * @param element - the WebElement whose text will be compared to an expected value after removing the + * whitespaces on this text + * @param expectedString - the expected value of the WebElement's text on which a whitespace removal is also done + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForElementTextEqualsString_IgnoreWhitespaces(WebElement element, String expectedString, WebDriver + driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition elementTextEqualsString = arg0 -> element.getText().replaceAll("\\s", "").equals + (expectedString.replaceAll("\\s", "")); + wait.until(elementTextEqualsString); + } + + /** + * Method that waits for the text on the element to contain an expected String. + * Checks whether the value resulted from getText() on the element contains the expected String. + * Will wait for up to the TIMEOUT number of seconds for the text on the element to contain the expected one. + * + * @param element - the WebElement whose text will be checked + * @param expectedString - the value expected to be contained in the WebElement's text + * @param driver - the WebDriver instance + */ + public void waitForElementTextContainsString(WebElement element, String expectedString, WebDriver driver) { + waitForElementTextContainsString(element, expectedString, driver, TIMEOUT); + } + + /** + * Method that waits for the text on the element to contain an expected String. + * Checks whether the value resulted from getText() on the element contains the expected String. + * Will wait for up to the specifiedTimeout number of seconds for the text on the element to contain the expected + * one. + * + * @param element - the WebElement whose text will be checked + * @param expectedString - the value expected to be contained in the WebElement's text + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForElementTextContainsString(WebElement element, String expectedString, WebDriver driver, int + specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition elementTextContainsString = arg0 -> element.getText().contains(expectedString); + wait.until(elementTextContainsString); + } + + /** + * Method that waits for the text on the element to contain an expected String but ignoring the case of the two. + * Checks whether the value resulted from getText() on the element contains the expected String, but without taking + * into account the case of the two values. + * Therefore, for example 'tHis' will contain 'his'. + * Will wait for up to the TIMEOUT number of seconds for the text on the element to contain the expected one. + * + * @param element - the WebElement whose text will be checked + * @param expectedString - the value expected to be part of the WebElement's text, ignoring the case + * @param driver - the WebDriver instance + */ + public void waitForElementTextContainsString_IgnoreCase(WebElement element, String expectedString, WebDriver + driver) { + waitForElementTextContainsString_IgnoreCase(element, expectedString, driver, TIMEOUT); + } + + /** + * Method that then waits for the text on the element to contain an + * expected String but ignoring the case of the two. + * Checks whether the value resulted from getText() on the element contains the expected String, but without taking + * into account the case of the two values. + * Therefore, for example 'tHis' will contain 'his'. + * Will wait for up to the specifiedTimeout number of seconds for the text on the element to be the expected one. + * + * @param element - the WebElement whose text will be checked + * @param expectedString - the value expected to be part of the WebElement's text, ignoring the case + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForElementTextContainsString_IgnoreCase(WebElement element, String expectedString, WebDriver driver, + int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition elementTextContainsString = arg0 -> element.getText().toLowerCase().contains + (expectedString.toLowerCase()); + wait.until(elementTextContainsString); + } + + /** + * Method that waits for the text on the element (whose + * whitespaces are removed) to contain an expected String (whose whitespaces are also removed). + * Basically, does a getText() on the WebElement, removes all whitespaces from this resulting String, then checks + * that this value contains another String that has no whitespaces. + * Whitespaces include: space, new line, tab. + * When calling the method, the expectedString can contain whitespaces, as they are removed inside this method. + * Therefore, 'this string here' will contain 'str ing here'. + * Will wait for up to the TIMEOUT number of seconds for the text on the element to be the expected one. + * + * @param element - the WebElement whose text will be compared to an expected value after removing the + * whitespaces on this text + * @param expectedString - the value expected to be part of the WebElement's text on which a whitespace removal is + * also done + * @param driver - the WebDriver instance + */ + public void waitForElementTextContainsString_IgnoreWhitespaces(WebElement element, String expectedString, WebDriver + driver) { + waitForElementTextContainsString_IgnoreWhitespaces(element, expectedString, driver, TIMEOUT); + } + + /** + * Method that waits for the text on the element (whose + * whitespaces are removed) to contain an expected String (whose whitespaces are also removed). + * Basically, does a getText() on the WebElement, removes all whitespaces from this resulting String, then checks + * that this value contains another String that has no whitespaces. + * Whitespaces include: space, new line, tab. + * When calling the method, the expectedString can contain whitespaces, as they are removed inside this method. + * Therefore, 'this string here' will contain 'str ing here'. + * Will wait for up to the specifiedTimeout number of seconds for the text on the element to be the expected one. + * + * @param element - the WebElement whose text will be compared to an expected value after removing the + * whitespaces on this text + * @param expectedString - the value expected to be part of the WebElement's text on which a whitespace removal is + * also done + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForElementTextContainsString_IgnoreWhitespaces(WebElement element, String expectedString, WebDriver + driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition elementTextContainsString = arg0 -> element.getText().replaceAll("\\s", "").contains + (expectedString.replaceAll("\\s", "")); + wait.until(elementTextContainsString); + } + + // Element attribute methods + + /** + * Method that waits for an element's specified attribute's value to equal another specified String. + * Compares the value resulted from getAttribute(nameOfAttribute) on the element with the expected String. + * Will wait for up to the TIMEOUT number of seconds for an element's attribute value to equal an expected String. + * + * @param element - the WebElement whose attribute we are interested in + * @param attribute - the attribute whose value needs to be compared to another value + * @param expectedString - the expected value of the WebElement's attribute + * @param driver - the WebDriver instance + */ + public void waitForElementAttributeEqualsString(WebElement element, String attribute, String expectedString, + WebDriver driver) { + waitForElementAttributeEqualsString(element, attribute, expectedString, driver, TIMEOUT); + } + + /** + * Method that waits for an element's specified attribute's value to equal another specified String. + * Compares the value resulted from getAttribute(nameOfAttribute) on the element with the expected String. + * Will wait for up to the specified number of seconds for an element's attribute value to equal an expected String. + * + * @param element - the WebElement whose attribute we are interested in + * @param attribute - the attribute whose value needs to be compared to another value + * @param expectedString - the expected value of the WebElement's attribute + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForElementAttributeEqualsString(WebElement element, String attribute, String expectedString, + WebDriver driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition elementAttributeEqualsString = arg0 -> element.getAttribute(attribute).equals + (expectedString); + wait.until(elementAttributeEqualsString); + } + + /** + * Method that waits for an element's specified attribute's value to equal another specified String, no matter + * the case of the actual or expected value. + * Compares the value resulted from getAttribute(nameOfAttribute) on the element with the expected String, + * ignoring the cases. + * Will wait for up to the TIMEOUT number of seconds for an element's attribute value to equal an expected + * String, ignoring the cases. + * + * @param element - the WebElement whose attribute we are interested in + * @param attribute - the attribute whose value needs to be compared to another value + * @param expectedString - the expected value of the WebElement's attribute, case insensitive + * @param driver - the WebDriver instance + */ + public void waitForElementAttributeEqualsString_IgnoreCase(WebElement element, String attribute, String + expectedString, WebDriver driver) { + waitForElementAttributeEqualsString_IgnoreCase(element, attribute, expectedString, driver, TIMEOUT); + } + + /** + * Method that waits for an element's specified attribute's value to equal another specified String, no matter + * the case of the actual or expected value. + * Compares the value resulted from getAttribute(nameOfAttribute) on the element with the expected String, + * ignoring the cases. + * Will wait for up to the TIMEOUT number of seconds for an element's attribute value to equal an expected String. + * + * @param element - the WebElement whose attribute we are interested in + * @param attribute - the attribute whose value needs to be compared to another value + * @param expectedString - the expected value of the WebElement's attribute, case insensitive + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForElementAttributeEqualsString_IgnoreCase(WebElement element, String attribute, String + expectedString, WebDriver driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition elementAttributeEqualsStringIgnoreCase = arg0 -> element.getAttribute(attribute) + .equalsIgnoreCase + (expectedString); + wait.until(elementAttributeEqualsStringIgnoreCase); + } + + /** + * Method that waits for an element's attribute value (whose whitespaces are removed) to equal an expected + * String (whose whitespaces are also removed). + * Basically, does a getAttribute(nameOfAttribute) on the WebElement, removes all whitespaces from this resulting + * String, then compares this value to another String that contains no whitespaces. + * When calling the method, the expectedString can contain whitespaces, as they are removed inside this method. + * Whitespaces include: space, new line, tab. + * Having said that, only the non whitespace characters are compared. + * Therefore, 'this string here' will equal 'this string here'. + * Will wait for up to the TIMEOUT number of seconds for expected condition to occur. + * + * @param element - the WebElement whose attribute will be verified + * @param attribute - the attribute whose value will be verified + * @param expectedString - the expected value of the WebElement's attribute on which a whitespace removal is also + * done + * @param driver - the WebDriver instance + */ + public void waitForElementAttributeEqualsString_IgnoreWhitespaces(WebElement element, String attribute, String + expectedString, WebDriver driver) { + waitForElementAttributeEqualsString_IgnoreWhitespaces(element, attribute, expectedString, driver, TIMEOUT); + } + + /** + * Method that waits for an element's attribute value (whose whitespaces are removed) to equal an expected + * String (whose whitespaces are also removed). + * Basically, does a getAttribute(nameOfAttribute) on the WebElement, removes all whitespaces from this resulting + * String, then compares this value to another String that contains no whitespaces. + * When calling the method, the expectedString can contain whitespaces, as they are removed inside this method. + * Whitespaces include: space, new line, tab. + * Having said that, only the non whitespace characters are compared. + * Therefore, 'this string here' will equal 'this string here'. + * Will wait for up to the specified timeout number of seconds for expected condition to occur. + * + * @param element - the WebElement whose attribute will be verified + * @param attribute - the attribute whose value will be verified + * @param expectedString - the expected value of the WebElement's attribute on which a whitespace removal is also + * done + * @param driver - the WebDriver instance + * @param specifiedTimeout - the amount of seconds to wait for the condition to occur + */ + public void waitForElementAttributeEqualsString_IgnoreWhitespaces(WebElement element, String attribute, String + expectedString, WebDriver driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition elementAttributeEqualsStringIW = arg0 -> element.getAttribute(attribute) + .replaceAll("\\s", "").equals(expectedString.replaceAll("\\s", "")); + wait.until(elementAttributeEqualsStringIW); + } + + /** + * Method that waits for an element's specified attribute's value to contain another specified String. + * Compares the value resulted from getAttribute(nameOfAttribute) on the element with the expected String. + * Will wait for up to the TIMEOUT number of seconds for condition to occur. + * + * @param element - the WebElement whose attribute we are interested in + * @param attribute - the attribute whose value needs to be compared to another value + * @param expectedString - the expected value of the WebElement's attribute + * @param driver - the WebDriver instance + */ + public void waitForElementAttributeContainsString(WebElement element, String attribute, String expectedString, + WebDriver driver) { + waitForElementAttributeContainsString(element, attribute, expectedString, driver, TIMEOUT); + } + + /** + * Method that waits for an element's specified attribute's value to contain another specified String. + * Compares the value resulted from getAttribute(nameOfAttribute) on the element with the expected String. + * Will wait for up to the specified number of seconds for the condition to occur. + * + * @param element - the WebElement whose attribute we are interested in + * @param attribute - the attribute whose value needs to be compared to another value + * @param expectedString - the expected value of the WebElement's attribute + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForElementAttributeContainsString(WebElement element, String attribute, String expectedString, + WebDriver driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition elementAttributeContainsString = arg0 -> element.getAttribute(attribute).contains + (expectedString); + wait.until(elementAttributeContainsString); + } + + /** + * Method that waits for an element's specified attribute's value to equal another specified String, no matter + * the case of the actual or expected value. + * Compares the value resulted from getAttribute(nameOfAttribute) on the element with the expected String, + * ignoring the cases. + * Will wait for up to the TIMEOUT number of seconds for an element's attribute value to equal an expected String. + * + * @param element - the WebElement whose attribute we are interested in + * @param attribute - the attribute whose value needs to be compared to another value + * @param expectedString - the expected value of the WebElement's attribute, case insensitive + * @param driver - the WebDriver instance + */ + public void waitForElementAttributeContainsString_IgnoreCase(WebElement element, String attribute, String + expectedString, WebDriver driver) { + waitForElementAttributeContainsString_IgnoreCase(element, attribute, expectedString, driver, TIMEOUT); + } + + /** + * Method that waits for an element's specified attribute's value to contain another specified String, no matter + * the case of the actual or expected value. + * Compares the value resulted from getAttribute(nameOfAttribute) on the element with the expected String, + * ignoring the cases. + * Will wait for up to the specifiedTimeout number of seconds for the expected condition to occur. + * + * @param element - the WebElement whose attribute we are interested in + * @param attribute - the attribute whose value needs to be compared to another value + * @param expectedString - the expected value of the WebElement's attribute, case insensitive + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForElementAttributeContainsString_IgnoreCase(WebElement element, String attribute, String + expectedString, WebDriver driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition elementAttributeContainsStringIC = arg0 -> element.getAttribute(attribute) + .toLowerCase() + .contains(expectedString.toLowerCase()); + wait.until(elementAttributeContainsStringIC); + } + + /** + * Method that waits for an element's attribute value (whose whitespaces are removed) to equal an expected + * String (whose whitespaces are also removed). + * Basically, does a getAttribute(nameOfAttribute) on the WebElement, removes all whitespaces from this resulting + * String, then compares this value to another String that contains no whitespaces. + * When calling the method, the expectedString can contain whitespaces, as they are removed inside this method. + * Whitespaces include: space, new line, tab. + * Having said that, only the non whitespace characters are compared. + * Therefore, 'this string here' will equal 'this string here'. + * Will wait for up to the TIMEOUT timeout number of seconds for expected condition to occur. + * + * @param element - the WebElement whose attribute will be verified + * @param attribute - the attribute whose value will be verified + * @param expectedString - the expected value of the WebElement's attribute on which a whitespace removal is also + * done + * @param driver - the WebDriver instance + */ + public void waitForElementAttributeContainsString_IgnoreWhitespaces(WebElement element, String attribute, String + expectedString, WebDriver driver) { + waitForElementAttributeContainsString_IgnoreWhitespaces(element, attribute, expectedString, driver, TIMEOUT); + } + + /** + * Method that waits for an element's attribute value (whose whitespaces are removed) to contain an expected + * String (whose whitespaces are also removed). + * Basically, does a getAttribute(nameOfAttribute) on the WebElement, removes all whitespaces from this resulting + * String, then compares this value to another String that contains no whitespaces. + * When calling the method, the expectedString can contain whitespaces, as they are removed inside this method. + * Whitespaces include: space, new line, tab. + * Having said that, only the non whitespace characters are compared. + * Will wait for up to the specified timeout number of seconds for expected condition to occur. + * + * @param element - the WebElement whose attribute will be verified + * @param attribute - the attribute whose value will be verified + * @param expectedString - the expected value of the WebElement's attribute on which a whitespace removal is also + * done + * @param driver - the WebDriver instance + * @param specifiedTimeout - the amount of seconds to wait for the condition to occur + */ + public void waitForElementAttributeContainsString_IgnoreWhitespaces(WebElement element, String attribute, String + expectedString, WebDriver driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition elementAttributeContainsStringIW = arg0 -> element.getAttribute(attribute) + .replaceAll("\\s", "") + .contains(expectedString.replaceAll("\\s", "")); + wait.until(elementAttributeContainsStringIW); + } + + + // URL wait methods + + /** + * Wait for a URL to open in the browser and for the page to load completely. + * Wait for TIMEOUT number of seconds. + * + * @param url - the URL to open + * @param driver - the WebDriver instance + */ + public void waitForUrl(String url, WebDriver driver) { + waitForUrl(url, driver, TIMEOUT); + } + + /** + * Wait for a URL to open in the browser and for the page to load completely. + * Wait for the specifiedTimeout number of seconds. + * + * @param url - the URL to open + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForUrl(String url, WebDriver driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition urlIsCorrect = arg0 -> driver.getCurrentUrl().equals(url); + wait.until(urlIsCorrect); + waitForPageLoadComplete(driver, specifiedTimeout); + } + + /** + * Wait for a URL containing a specified String to open in the browser. + * The URL will not equal the specified String. Just contain it. + * Wait for TIMEOUT number of seconds. + * + * @param expectedString - the String that needs to be included in the URL + * @param driver - the WebDriver instance + */ + public void waitForUrlContains(String expectedString, WebDriver driver) { + waitForUrlContains(expectedString, driver, TIMEOUT); + } + + /** + * Wait for a URL containing a specified String to open in the browser. + * The URL will not equal the specified String. Just contain it. + * Wait for the specifiedTimeout number of seconds. + * + * @param expectedString - the String that needs to be included in the URL + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForUrlContains(String expectedString, WebDriver driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition urlIsCorrect = arg0 -> driver.getCurrentUrl().contains(expectedString); + wait.until(urlIsCorrect); + waitForPageLoadComplete(driver, specifiedTimeout); + } + + /** + * Wait for an expected URL to load in the browser, but ignore the case of the url. + * Compares a lower case value of the actual url in the browser with the lower case value of the expected url. + * Wait for the TIMEOUT number of seconds. + * + * @param url - the url expected to load in the browser, ignoring its case + * @param driver - the WebDriver instance + */ + public void waitForUrl_IgnoreCase(String url, WebDriver driver) { + waitForUrl_IgnoreCase(url, driver, TIMEOUT); + } + + /** + * Wait for an expected URL to load in the browser, but ignore the case of the url. + * Compares a lower case value of the actual url in the browser with the lower case value of the expected url. + * Wait for the specifiedTimeout number of seconds. + * + * @param url - the url expected to load in the browser, ignoring its case + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForUrl_IgnoreCase(String url, WebDriver driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition urlIsCorrect = arg0 -> driver.getCurrentUrl().toLowerCase().equals(url.toLowerCase + ()); + wait.until(urlIsCorrect); + waitForPageLoadComplete(driver, specifiedTimeout); + } + + /** + * Wait for a URL containing a specified String to open in the browser, ignoring the case of the url. + * Checks whether a lower case value of the actual URL contains a lower case value of the expected String. + * Wait for the TIMEOUT number of seconds. + * + * @param expectedString - the String that needs to be included in the URL + * @param driver - the WebDriver instance + */ + public void waitForUrlContains_IgnoreCase(String expectedString, WebDriver driver) { + waitForUrlContains_IgnoreCase(expectedString, driver, TIMEOUT); + } + + /** + * Wait for a URL containing a specified String to open in the browser, ignoring the case of the url. + * Checks whether a lower case value of the actual URL contains a lower case value of the expected String. + * Wait for the specifiedTimeout number of seconds. + * + * @param expectedString - the String that needs to be included in the URL + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForUrlContains_IgnoreCase(String expectedString, WebDriver driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition urlIsCorrect = arg0 -> driver.getCurrentUrl().toLowerCase().contains(expectedString + .toLowerCase()); + wait.until(urlIsCorrect); + waitForPageLoadComplete(driver, specifiedTimeout); + } + + /** + * Wait for the URL in the browser to start with a specified String. + * Please see the startsWith method from the String class for details on how this method determines whether a + * String starts with another one. + * Wait for the TIMEOUT number of seconds. + * + * @param expectedString - the expected String to be found at the start of the URL loaded in the browser + * @param driver - the WebDriver instance + */ + public void waitForUrlStartsWith(String expectedString, WebDriver driver) { + waitForUrlStartsWith(expectedString, driver, TIMEOUT); + } + + /** + * Wait for the URL in the browser to start with a specified String. + * Please see the startsWith method from the String class for details on how this method determines whether a + * String starts with another one. + * Wait for the specifiedTimeout number of seconds. + * + * @param expectedString - the expected String to be found at the start of the URL loaded in the browser + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void waitForUrlStartsWith(String expectedString, WebDriver driver, int specifiedTimeout) { + WebDriverWait wait = new WebDriverWait(driver, specifiedTimeout); + ExpectedCondition urlIsCorrect = arg0 -> driver.getCurrentUrl().startsWith(expectedString); + wait.until(urlIsCorrect); + waitForPageLoadComplete(driver, specifiedTimeout); + } + + // CLICK AND WAIT METHODS + + /** + * Click on a WebElement and than wait for a specified URL to load in the browser. + * Wait for TIMEOUT number of seconds. + * + * @param element - the WebElement to click on + * @param url - the URL you need are waiting to load + * @param driver - the WebDriver instance + */ + public void clickElementAndWaitForUrl(WebElement element, String url, WebDriver driver) { + clickElementAndWaitForUrl(element, url, driver, TIMEOUT); + } + + /** + * Click on a WebElement and than wait for a specified URL to load in the browser. + * Wait for the specifiedTimeout number of seconds. + * + * @param element - the WebElement to click on + * @param url - the URL you need are waiting to load + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void clickElementAndWaitForUrl(WebElement element, String url, WebDriver driver, int specifiedTimeout) { + click(element, driver); + waitForUrl(url, driver, specifiedTimeout); + } + + /** + * Click on a WebElement and than wait for the URL that loads in the browser to contain a specified String. + * Wait for the TIMEOUT number of seconds. + * + * @param element - the WebElement to click on + * @param expectedStringInUrl - the String you expect to be contained in the URL that loads after clicking the + * WebElement + * @param driver - the WebDriver instance + */ + public void clickElementAndWaitForUrlContains(WebElement element, String expectedStringInUrl, WebDriver driver) { + clickElementAndWaitForUrlContains(element, expectedStringInUrl, driver, TIMEOUT); + + } + + /** + * Click on a WebElement and than wait for the URL that loads in the browser to contain a specified String. + * Wait for the specifiedTimeout number of seconds. + * + * @param element - the WebElement to click on + * @param expectedStringInUrl - the String you expect to be contained in the URL that loads after clicking the + * WebElement + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void clickElementAndWaitForUrlContains(WebElement element, String expectedStringInUrl, WebDriver driver, int + specifiedTimeout) { + click(element, driver); + waitForUrlContains(expectedStringInUrl, driver, specifiedTimeout); + } + + /** + * Click on a WebElement and than wait for a specified URL to load in the browser, ignoring the case. + * Wait for TIMEOUT number of seconds. + * + * @param element - the WebElement to click on + * @param url - the URL you need are waiting to load + * @param driver - the WebDriver instance + */ + public void clickElementAndWaitForUrl_IgnoreCase(WebElement element, String url, WebDriver driver) { + clickElementAndWaitForUrl_IgnoreCase(element, url, driver, TIMEOUT); + } + + /** + * Click on a WebElement and than wait for a specified URL to load in the browser, ignoring the case. + * Wait for the specifiedTimeout number of seconds. + * + * @param element - the WebElement to click on + * @param url - the URL you need are waiting to load + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void clickElementAndWaitForUrl_IgnoreCase(WebElement element, String url, WebDriver driver, int + specifiedTimeout) { + click(element, driver); + waitForUrl_IgnoreCase(url, driver, specifiedTimeout); + } + + /** + * Click on a WebElement and than wait for the URL that loads in the browser to contain a specified String, + * ignoring the case. + * Wait for the TIMEOUT number of seconds. + * + * @param element - the WebElement to click on + * @param expectedStringInUrl - the String you expect to be contained in the URL that loads after clicking the + * WebElement + * @param driver - the WebDriver instance + */ + public void clickElementAndWaitForUrlContains_IgnoreCase(WebElement element, String expectedStringInUrl, WebDriver + driver) { + clickElementAndWaitForUrlContains_IgnoreCase(element, expectedStringInUrl, driver, TIMEOUT); + + } + + /** + * Click on a WebElement and than wait for the URL that loads in the browser to contain a specified String, + * ignoring the case. + * Wait for the specifiedTimeout number of seconds. + * + * @param element - the WebElement to click on + * @param expectedStringInUrl - the String you expect to be contained in the URL that loads after clicking the + * WebElement + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void clickElementAndWaitForUrlContains_IgnoreCase(WebElement element, String expectedStringInUrl, WebDriver + driver, int specifiedTimeout) { + click(element, driver); + waitForUrlContains_IgnoreCase(expectedStringInUrl, driver, specifiedTimeout); + } + + /** + * Click on a WebElement and wait for the URL in the browser to start with an expected String. + * Wait for the TIMEOUT number of seconds. + * + * @param element - the WebElement to click on + * @param expectedString - the String you expected the URL in the browser to start with + * @param driver - the WebDriver instance + */ + public void clickElementAndWaitForUrlStartsWith(WebElement element, String expectedString, WebDriver + driver) { + clickElementAndWaitForUrlStartsWith(element, expectedString, driver, TIMEOUT); + } + + /** + * Click on a WebElement and wait for the URL in the browser to start with an expected String. + * Wait for the specifiedTimeout number of seconds. + * + * @param element - the WebElement to click on + * @param expectedString - the String you expected the URL in the browser to start with + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void clickElementAndWaitForUrlStartsWith(WebElement element, String expectedString, WebDriver + driver, int specifiedTimeout) { + click(element, driver); + waitForUrlStartsWith(expectedString, driver, specifiedTimeout); + } + + /** + * Click on an element and wait for a page refresh. To be used when you want the same page to reload after + * clicking an element. For redirect to other pages, use wait for url methods. + * Wait for the TIMEOUT number of seconds. + * + * @param element - the WebElement to click on + * @param driver - the WebDriver instance + */ + public void clickElementAndWaitForPageLoadComplete(WebElement element, WebDriver driver) { + clickElementAndWaitForPageLoadComplete(element, driver, TIMEOUT); + } + + /** + * Click on an element and wait for a page refresh. To be used when you want the same page to reload after + * clicking an element. For redirect to other pages, use wait for url methods. + * Wait for the specifiedTimeout number of seconds. + * + * @param element - the WebElement to click on + * @param driver - the WebDriver instance + * @param specifiedTimeout - amount of seconds you want to wait for + */ + public void clickElementAndWaitForPageLoadComplete(WebElement element, WebDriver driver, int specifiedTimeout) { + click(element, driver, specifiedTimeout); + waitForPageLoadComplete(driver, specifiedTimeout); + } + +} \ No newline at end of file diff --git a/src/main/resources/forFrameAsWebElement.html b/src/main/resources/forFrameAsWebElement.html new file mode 100644 index 0000000..2dc6bfa --- /dev/null +++ b/src/main/resources/forFrameAsWebElement.html @@ -0,0 +1,10 @@ + + + +
+ +
+ \ No newline at end of file diff --git a/src/main/resources/forFrameWithId.html b/src/main/resources/forFrameWithId.html new file mode 100644 index 0000000..2d1fa79 --- /dev/null +++ b/src/main/resources/forFrameWithId.html @@ -0,0 +1,11 @@ + + + +
+ +
+ \ No newline at end of file diff --git a/src/main/resources/forFrameWithIndex.html b/src/main/resources/forFrameWithIndex.html new file mode 100644 index 0000000..b0969ed --- /dev/null +++ b/src/main/resources/forFrameWithIndex.html @@ -0,0 +1,8 @@ + + + +
+ +
\ No newline at end of file diff --git a/src/main/resources/fullPage.html b/src/main/resources/fullPage.html index 58694cc..4b56718 100644 --- a/src/main/resources/fullPage.html +++ b/src/main/resources/fullPage.html @@ -6,13 +6,6 @@ @@ -100,6 +93,11 @@

Sibling

Level 2 siblings with tags identical to ALPHA item

Sibling

+
​ +
Sibling
​ +
ALPHA
​ +
Sibling
​ +
Dropdown: A checkbox
+ The radio button + +

{{successtext}}

+ Link +
+
+

sendKeys()

+ + +
+
+
+

getText()

+
+ This is the module with id +
+
H5 text
+ Link text + + +
+
+
+ +
+ +
+

getAttribute()

+
+
Using IDs
+ This is the module with id +
+ +
+ The checked checkbox
+ The unchecked checkbox
+ The radio button +
+
+ +
+
+
+

Select

+ + + +
+
+

getCSSValue()

+
+ + diff --git a/src/main/resources/mainPage.html b/src/main/resources/mainPage.html new file mode 100644 index 0000000..af66b03 --- /dev/null +++ b/src/main/resources/mainPage.html @@ -0,0 +1,20 @@ + + + + + +
+
+

Welcome to the main page

+
+
+ +
+

This is the first page

+ Go to second page + Go to third page +
+ + + + diff --git a/src/main/resources/secondPage.html b/src/main/resources/secondPage.html new file mode 100644 index 0000000..49f0ad8 --- /dev/null +++ b/src/main/resources/secondPage.html @@ -0,0 +1,18 @@ + + + + + +
+
+

Welcome to the second page

+
+
+ +
+

This is the second page

+
+ + + + diff --git a/src/main/resources/thirdPage.html b/src/main/resources/thirdPage.html new file mode 100644 index 0000000..caf23a3 --- /dev/null +++ b/src/main/resources/thirdPage.html @@ -0,0 +1,18 @@ + + + + + +
+
+

Welcome to the third page

+
+
+ +
+

This is the third page

+
+ + + + diff --git a/src/main/resources/userPrompts.html b/src/main/resources/userPrompts.html new file mode 100644 index 0000000..ea1d4e3 --- /dev/null +++ b/src/main/resources/userPrompts.html @@ -0,0 +1,40 @@ + + +User prompts + + + + + + + +

+ + + + + diff --git a/src/main/resources/withIframes.html b/src/main/resources/withIframes.html new file mode 100644 index 0000000..dfa9541 --- /dev/null +++ b/src/main/resources/withIframes.html @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/src/test/java/backup/tutorialsolution/windows/WindowsTest.java b/src/test/java/backup/tutorialsolution/windows/WindowsTest.java new file mode 100644 index 0000000..791d478 --- /dev/null +++ b/src/test/java/backup/tutorialsolution/windows/WindowsTest.java @@ -0,0 +1,119 @@ +package backup.tutorialsolution.windows; + +import browser.BrowserGetter; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.PageFactory; +import tutorialsolution.pages.WindowsPage; + +import java.io.File; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class WindowsTest { + private WebDriver driver; + private BrowserGetter browserGetter = new BrowserGetter(); + private WindowsPage page; + + @BeforeEach + public void beforeEach() { + //initialize the Chrome browser here + driver = browserGetter.getChromeDriver(); + //initialize page object class + page = PageFactory.initElements(driver, WindowsPage.class); + driver.get("https://imalittletester.godaddysites.com/mainpage"); + driver.switchTo().defaultContent(); + driver.switchTo().frame(0); + } + + @AfterEach + public void afterEach() { + driver.quit(); + } + + @Test + public void savingHandlesToSet() { + //before opening any new page, get the handle of the main page + //it will be needed later in the tes + String mainPageWindowHandle = driver.getWindowHandle(); + + //open two new pages in new windows/tabs + page.linkToSecondPage.click(); + page.linkToThirdPage.click(); + //two new windows or tabs are open + //first call the 'getWindowHandles()' method which returns a set of window handles + Set setOfWindowHandles = driver.getWindowHandles(); + //check that there are 3 open windows + assertEquals(3, setOfWindowHandles.size()); + + //check that the

element on the second page (with url containing 'secondPage') is correct; also check + // that there exists an open page with the url corresponding to the expected second page + boolean foundWindow = false; + for (String handle : setOfWindowHandles) { + driver.switchTo().window(handle); + if (driver.getCurrentUrl().contains("secondpage")) { + driver.switchTo().defaultContent(); + driver.switchTo().frame(0); + assertEquals("Welcome to the second page", page.h1Element.getText()); + foundWindow = true; + } + } + //if this check would not be done, the for loop would exit successfully both if the expected windows was or + // was not found + assertTrue(foundWindow, "No such window was open"); + + //switch to the main page and check that the switch was made correctly + driver.switchTo().window(mainPageWindowHandle); + assertTrue(driver.getCurrentUrl().contains("mainpage")); + } + + @Test + public void closeOneOfOpenWindows() { + //from the main page click on both links, which will open two new windows + page.linkToSecondPage.click(); + page.linkToThirdPage.click(); + + //two new windows or tabs are open + //check that there are 3 open windows/tabs + assertEquals(3, driver.getWindowHandles().size()); + + //close second page window/tab + //iterate over list since you don't know which handle corresponds to second page + Set setOfWindowHandles = driver.getWindowHandles(); + for (String handle : setOfWindowHandles) { + driver.switchTo().window(handle); + if (driver.getCurrentUrl().contains("secondpage")) { + driver.close(); + } + } + //check that there are now 2 open windows/tabs + assertEquals(2, driver.getWindowHandles().size()); + + //in order to continue working with the remaining pages, you will need to switch to them + //keep in mind that the handle of the second page is not valid anymore + //therefore you should regenerate the list of handles, for it to only contain the handles of the currently + // open pages + setOfWindowHandles = driver.getWindowHandles(); + //switch to the main page + for (String handle : setOfWindowHandles) { + driver.switchTo().window(handle); + if (driver.getCurrentUrl().contains("mainpage")) { + break; + } + } + + //open the second page again. It will now have a new handle. Therefore the list of handles needs to be + // regenerated if you want to work with any of the windows + //if not, just check that now there are three open windows + driver.switchTo().defaultContent(); + driver.switchTo().frame(0); + page.linkToSecondPage.click(); + assertEquals(3, driver.getWindowHandles().size()); + } +} diff --git a/src/test/java/gotoams/SeOOTest.java b/src/test/java/gotoams/SeOOTest.java new file mode 100644 index 0000000..6fbf6f0 --- /dev/null +++ b/src/test/java/gotoams/SeOOTest.java @@ -0,0 +1,51 @@ +package gotoams; + +import browser.BrowserGetter; +import objects.Article; +import objects.Image; +import objects.Link; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.PageFactory; +import pages.SeOOPage; + +import java.io.File; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class SeOOTest { + private BrowserGetter browserGetter = new BrowserGetter(); + private WebDriver driver; + private SeOOPage page; + + @BeforeAll + public void beforeAll() { + driver = browserGetter.getChromeDriver(); + page = PageFactory.initElements(driver, SeOOPage.class); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } + + @Test + public void firstTest() throws InterruptedException { + driver.get(new File("src/main/resources/goto.html").getAbsolutePath()); + Thread.sleep(2000); + Article expectedArticle = new Article(new Image("http://www.gotoams.nl", "250", "250"), "Tags: smth", + new Link("https://imalittletester.com", "Testing")); + Article actualArticle = new Article(page.elements.get(0)); + assertEquals(expectedArticle, actualArticle); + } +} + + + + + + diff --git a/src/test/java/ntd2019/CookieTest.java b/src/test/java/ntd2019/CookieTest.java new file mode 100644 index 0000000..e63ab9e --- /dev/null +++ b/src/test/java/ntd2019/CookieTest.java @@ -0,0 +1,48 @@ +package ntd2019; + +import browser.BrowserGetter; +import org.junit.jupiter.api.*; +import org.openqa.selenium.Cookie; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.PageFactory; +import pages.InteractionsPage; + +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class CookieTest { + private WebDriver driver; + private BrowserGetter browserGetter = new BrowserGetter(); + + @BeforeAll + public void beforeAll() { + driver = browserGetter.getChromeDriver(); + } + + @BeforeEach + public void beforeEach() { + driver.get("http://example.com/"); + driver.manage().deleteAllCookies(); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } + + @Test + public void addCookie() { + Cookie firstCookie = new Cookie("firstCookie", "goodCookie"); + Cookie secondCookie = new Cookie("secondCookie", "badCookie"); + driver.manage().addCookie(firstCookie); + driver.manage().addCookie(secondCookie); + driver.navigate().refresh(); + + assertEquals(2, driver.manage().getCookies().size()); + + driver.manage().deleteAllCookies(); + assertEquals(0, driver.manage().getCookies().size()); + } +} diff --git a/src/test/java/ntd2019/Ntd2019Test.java b/src/test/java/ntd2019/Ntd2019Test.java new file mode 100644 index 0000000..356a985 --- /dev/null +++ b/src/test/java/ntd2019/Ntd2019Test.java @@ -0,0 +1,130 @@ +package ntd2019; + +import browser.BrowserGetter; +import org.junit.jupiter.api.*; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.PageFactory; +import org.openqa.selenium.support.ui.Select; +import org.openqa.selenium.support.ui.Wait; +import pages.InteractionsPage; +import waiter.Waiter; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class Ntd2019Test { + private WebDriver driver; + private BrowserGetter browserGetter = new BrowserGetter(); + private InteractionsPage page; +private Waiter waiter = new Waiter(); + + @BeforeAll + public void beforeAll() { + driver = browserGetter.getDriver(); + page = PageFactory.initElements(driver, InteractionsPage.class); + waiter.get("https://imalittletester.godaddysites.com/interactions", driver); + + } + + @BeforeEach + public void beforeEach() { + driver.switchTo().defaultContent(); + assertEquals("https://imalittletester.godaddysites.com/interactions", driver.getCurrentUrl()); + driver.switchTo().frame(0); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } + + @Test + public void clickTest() throws InterruptedException { + waiter.click(page.checkbox, driver); + waiter.waitForElementAttributeEqualsString(page.checkbox, "checked", "true", driver); + +// page.checkbox.click(); +// Thread.sleep(1000); +// assertNull(page.checkbox.getAttribute("checked")); +// +// assertNull(page.radioButton.getAttribute("checked")); +// page.radioButton.click(); +// Thread.sleep(1000); +// assertEquals("true", page.radioButton.getAttribute("checked")); +// + + waiter.click(page.link, driver); + waiter.waitForUrl("https://www.example.com/", driver); + waiter.waitForElementTextEqualsString(page.h1, "Example Domain", driver); + + } + + @Test + public void sendKeysTest() throws InterruptedException { + page.textInput.sendKeys("007"); + Thread.sleep(1000); + + page.textInput.clear(); + Thread.sleep(1000); + + page.textInput.sendKeys("009"); + Thread.sleep(1000); + + page.textArea.sendKeys("008"); + + System.out.println("---------" + page.predefinedTextArea.getText()); + page.predefinedTextArea.clear(); + page.predefinedTextArea.sendKeys("New text"); + Thread.sleep(5000); + + } + + @Test + public void getAttributeTest() { + assertEquals("100", page.img.getAttribute("width")); + System.out.println(page.disabledButton.getAttribute("disabled")); + } + + @Test + public void getTextTest() { + assertEquals("Link", page.link.getText()); + assertEquals("H5 text", page.h5.getText()); + assertEquals("dummy", page.div.getText()); + } + + @Test + public void getCssValueTest() { + System.out.println(page.container.getCssValue("width")); + System.out.println(page.h2.getCssValue("font-family")); + } + + @Test + public void selectFromDropdown() throws InterruptedException { + Select refreshmentSelect = new Select(page.refreshmentSelectWebElement); + refreshmentSelect.selectByValue("4"); + refreshmentSelect.selectByVisibleText("Sparkling Water"); + Thread.sleep(4000); + refreshmentSelect.deselectAll(); + Thread.sleep(4000); + } + + @Test + public void navigation() throws InterruptedException { + page.link.click(); + Thread.sleep(3000); + assertEquals("https://www.example.com/", driver.getCurrentUrl()); + driver.navigate().back(); + Thread.sleep(3000); + assertTrue(driver.getCurrentUrl().contains("interactions")); + + driver.navigate().forward(); + Thread.sleep(3000); + assertEquals("https://www.example.com/", driver.getCurrentUrl()); + + driver.navigate().refresh(); + Thread.sleep(3000); + assertEquals("https://www.example.com/", driver.getCurrentUrl()); + } +} diff --git a/src/test/java/ntd2019/UserPromptsTest.java b/src/test/java/ntd2019/UserPromptsTest.java new file mode 100644 index 0000000..23a752f --- /dev/null +++ b/src/test/java/ntd2019/UserPromptsTest.java @@ -0,0 +1,58 @@ +package ntd2019; + +import browser.BrowserGetter; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.openqa.selenium.Alert; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.PageFactory; +import tutorialsolution.pages.UserPromptsPage; + +import java.io.File; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; + +@TestInstance(PER_CLASS) +public class UserPromptsTest { + private final BrowserGetter browserGetter = new BrowserGetter(); + private UserPromptsPage page; + private WebDriver driver; + + @BeforeAll + public void beforeAll() { + //initialize the Chrome browser here + driver = browserGetter.getChromeDriver(); + //initialize page object class + page = PageFactory.initElements(driver, UserPromptsPage.class); + driver.get(new File("src/main/resources/userPrompts.html").getAbsolutePath()); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } + + @Test + public void prompts() throws InterruptedException { + page.alertButton.click(); + Thread.sleep(5000); + + driver.switchTo().alert().accept(); + Thread.sleep(5000); + + page.confirmButton.click(); + Thread.sleep(5000); + driver.switchTo().alert().accept(); + Thread.sleep(5000); + + page.confirmButton.click(); + Thread.sleep(5000); + driver.switchTo().alert().dismiss(); + Thread.sleep(5000); + + } + +} diff --git a/src/test/java/ntd2019/WindowsAndTabsTest.java b/src/test/java/ntd2019/WindowsAndTabsTest.java new file mode 100644 index 0000000..55e238b --- /dev/null +++ b/src/test/java/ntd2019/WindowsAndTabsTest.java @@ -0,0 +1,65 @@ +package ntd2019; + +import browser.BrowserGetter; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.PageFactory; +import pages.TabsPage; + +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class WindowsAndTabsTest { + private WebDriver driver; + private BrowserGetter browserGetter = new BrowserGetter(); + private TabsPage page; + + @BeforeAll + public void beforeAll() { + driver = browserGetter.getChromeDriver(); + page = PageFactory.initElements(driver, TabsPage.class); + driver.get("https://imalittletester.godaddysites.com/mainpage"); + driver.switchTo().frame(0); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } + + @Test + public void theTest() throws InterruptedException { + String mainWindowHandle = driver.getWindowHandle(); + page.linkToSecondPage.click(); + Thread.sleep(1000); + page.linkToThirdPage.click(); + Thread.sleep(1000); + + Set windowHandles = driver.getWindowHandles(); + assertEquals(3, windowHandles.size()); + for (String handle : windowHandles) { + driver.switchTo().window(handle); + if (driver.getCurrentUrl().contains("secondpage")) { + break; + } + } + driver.switchTo().frame(0); + System.out.println(page.h1.getText()); + driver.switchTo().defaultContent(); + driver.close(); + assertEquals(2, driver.getWindowHandles().size()); + + driver.switchTo().window(mainWindowHandle); + Thread.sleep(5000); + driver.switchTo().frame(0); + page.linkToSecondPage.click(); + Thread.sleep(1000); + assertEquals(3, driver.getWindowHandles().size()); + + } +} diff --git a/src/test/java/taudemo/SeleniumTest.java b/src/test/java/taudemo/SeleniumTest.java new file mode 100644 index 0000000..8b03e9d --- /dev/null +++ b/src/test/java/taudemo/SeleniumTest.java @@ -0,0 +1,47 @@ +package taudemo; + +import browser.BrowserGetter; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.openqa.selenium.WebDriver; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; + +//comment + +/** + * Javadoc + */ + +//todo +@TestInstance(PER_CLASS) +public class SeleniumTest { + private BrowserGetter browserGetter = new BrowserGetter(); + private WebDriver driver; + + @BeforeAll + public void beforeAll() { + driver = browserGetter.getChromeDriver(); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } + + @Test + public void openTheComPageAndCheckTheTitle() { + String expectedComTitle = "Example Domain"; + driver.get("https://www.example.com"); + assertEquals(expectedComTitle, driver.getTitle()); + } + @Test + public void openTheOrgPageAndCheckTheTitle() { + String expectedOrgTitle = "Example Domain"; + driver.get("https://www.example.org"); + assertEquals(expectedOrgTitle, driver.getTitle()); + } +} diff --git a/src/test/java/taudemo/WithAssertionsTest.java b/src/test/java/taudemo/WithAssertionsTest.java new file mode 100644 index 0000000..218dc7b --- /dev/null +++ b/src/test/java/taudemo/WithAssertionsTest.java @@ -0,0 +1,19 @@ +package taudemo; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class WithAssertionsTest { + + @Test + public void firstAssertion() { + assertEquals(2, 2); + } + + @Test + public void secondAssertion() { + assertEquals(45, 45); + } + +} diff --git a/src/test/java/taudemo/WithConfigurationTest.java b/src/test/java/taudemo/WithConfigurationTest.java new file mode 100644 index 0000000..c2302a1 --- /dev/null +++ b/src/test/java/taudemo/WithConfigurationTest.java @@ -0,0 +1,33 @@ +package taudemo; + +import browser.BrowserGetter; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.openqa.selenium.WebDriver; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; + +@TestInstance(PER_CLASS) +public class WithConfigurationTest { + private BrowserGetter browserGetter = new BrowserGetter(); + private WebDriver driver; + + @BeforeAll + public void beforeAll() { + driver = browserGetter.getDriver(); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } + + @Test + public void justATest() { + driver.get("https://www.example.com"); + assertEquals("Example Domain", driver.getTitle()); + } +} diff --git a/src/test/java/tutorialexercise/cookies/CookiesTest.java b/src/test/java/tutorialexercise/cookies/CookiesTest.java new file mode 100644 index 0000000..d116b71 --- /dev/null +++ b/src/test/java/tutorialexercise/cookies/CookiesTest.java @@ -0,0 +1,33 @@ +package tutorialexercise.cookies; + +import browser.BrowserGetter; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.openqa.selenium.WebDriver; + +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; + +@TestInstance(PER_CLASS) +public class CookiesTest { + private final BrowserGetter browserGetter = new BrowserGetter(); + private WebDriver driver; + + @BeforeAll + public void beforeAll() { + //initialize the Chrome browser here + driver = browserGetter.getChromeDriver(); + driver.get("https://example.com"); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } + + @Test + public void workingWithCookies() { + + } +} diff --git a/src/test/java/tutorialexercise/cssselectors/CSSSelectorsTest.java b/src/test/java/tutorialexercise/cssselectors/CSSSelectorsTest.java new file mode 100644 index 0000000..3d2285f --- /dev/null +++ b/src/test/java/tutorialexercise/cssselectors/CSSSelectorsTest.java @@ -0,0 +1,36 @@ +package tutorialexercise.cssselectors; + +import browser.BrowserGetter; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.TestInstance; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.PageFactory; +import tutorialsolution.pages.BasicPage; + +import java.io.File; + +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; + +@TestInstance(PER_CLASS) +public class CSSSelectorsTest { + private final BrowserGetter browserGetter = new BrowserGetter(); + private BasicPage page; + private WebDriver driver; + + @BeforeAll + public void beforeAll() { + //initialize the Chrome browser here + driver = browserGetter.getChromeDriver(); + //initialize page object class + page = PageFactory.initElements(driver, BasicPage.class); + driver.get(new File("src/main/resources/fullPage.html").getAbsolutePath()); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } + + +} diff --git a/src/test/java/tutorialexercise/elementinteraction/ElementInteractionTest.java b/src/test/java/tutorialexercise/elementinteraction/ElementInteractionTest.java new file mode 100644 index 0000000..1ea4c89 --- /dev/null +++ b/src/test/java/tutorialexercise/elementinteraction/ElementInteractionTest.java @@ -0,0 +1,72 @@ +package tutorialexercise.elementinteraction; + +import browser.BrowserGetter; +import org.junit.jupiter.api.*; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.PageFactory; +import tutorialsolution.pages.WebElementInteractionPage; + +import java.io.File; + +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; + +@TestInstance(PER_CLASS) +public class ElementInteractionTest { + private final BrowserGetter browserGetter = new BrowserGetter(); + private WebElementInteractionPage page; + private WebDriver driver; + + @BeforeAll + public void beforeAll() { + //initialize the Chrome browser here + driver = browserGetter.getChromeDriver(); + //initialize page object class + page = PageFactory.initElements(driver, WebElementInteractionPage.class); + } + + @BeforeEach + public void beforeEach() { + driver.get(new File("src/main/resources/interactions.html").getAbsolutePath()); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } + + @Test + public void click() { + + } + + @Test + public void sendKeys() { + + } + + @Test + public void clear() { + + } + + @Test + public void getText() { + + } + + @Test + public void getAttribute() { + + } + + @Test + public void select() { + + } + + @Test + public void getCssValue() { + + } +} + diff --git a/src/test/java/tutorialexercise/iframes/IframesTest.java b/src/test/java/tutorialexercise/iframes/IframesTest.java new file mode 100644 index 0000000..24419e4 --- /dev/null +++ b/src/test/java/tutorialexercise/iframes/IframesTest.java @@ -0,0 +1,35 @@ +package tutorialexercise.iframes; + +import browser.BrowserGetter; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.TestInstance; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.PageFactory; +import tutorialsolution.pages.BasicPage; + +import java.io.File; + +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; + +@TestInstance(PER_CLASS) +public class IframesTest { + private final BrowserGetter browserGetter = new BrowserGetter(); + private BasicPage page; + private WebDriver driver; + + @BeforeAll + public void beforeAll() { + //initialize the Chrome browser here + driver = browserGetter.getChromeDriver(); + //initialize page object class + page = PageFactory.initElements(driver, BasicPage.class); + driver.get(new File("src/main/resources/withIframes.html").getAbsolutePath()); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } + +} diff --git a/src/test/java/tutorialexercise/navigation/NavigationTest.java b/src/test/java/tutorialexercise/navigation/NavigationTest.java new file mode 100644 index 0000000..96cbdfd --- /dev/null +++ b/src/test/java/tutorialexercise/navigation/NavigationTest.java @@ -0,0 +1,37 @@ +package tutorialexercise.navigation; + +import browser.BrowserGetter; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.PageFactory; +import tutorialsolution.pages.WebElementInteractionPage; + +import java.io.File; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class NavigationTest { + private final BrowserGetter browserGetter = new BrowserGetter(); + private WebElementInteractionPage page; + private WebDriver driver; + + @BeforeAll + public void beforeAll() { + //initialize the Chrome browser here + driver = browserGetter.getChromeDriver(); + //initialize page object class + page = PageFactory.initElements(driver, WebElementInteractionPage.class); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } + + @Test + public void navigation() { + driver.get(new File("src/main/resources/interactions.html").getAbsolutePath()); + } +} diff --git a/src/test/java/tutorialexercise/openpage/OpenPageTest.java b/src/test/java/tutorialexercise/openpage/OpenPageTest.java new file mode 100644 index 0000000..439430f --- /dev/null +++ b/src/test/java/tutorialexercise/openpage/OpenPageTest.java @@ -0,0 +1,22 @@ +package tutorialexercise.openpage; + +import browser.BrowserGetter; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.openqa.selenium.WebDriver; + +public class OpenPageTest { + private final BrowserGetter browserGetter = new BrowserGetter(); + private WebDriver driver; + + @BeforeAll + public void beforeAll() { + //initialize the Chrome browser here + driver = browserGetter.getChromeDriver(); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } +} diff --git a/src/test/java/tutorialexercise/userprompts/UserPromptsTest.java b/src/test/java/tutorialexercise/userprompts/UserPromptsTest.java new file mode 100644 index 0000000..f2bf343 --- /dev/null +++ b/src/test/java/tutorialexercise/userprompts/UserPromptsTest.java @@ -0,0 +1,36 @@ +package tutorialexercise.userprompts; + +import browser.BrowserGetter; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.TestInstance; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.PageFactory; +import tutorialsolution.pages.UserPromptsPage; + +import java.io.File; + +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; + +@TestInstance(PER_CLASS) +public class UserPromptsTest { + private final BrowserGetter browserGetter = new BrowserGetter(); + private UserPromptsPage page; + private WebDriver driver; + + @BeforeAll + public void beforeAll() { + //initialize the Chrome browser here + driver = browserGetter.getChromeDriver(); + //initialize page object class + page = PageFactory.initElements(driver, UserPromptsPage.class); + driver.get(new File("src/main/resources/userPrompts.html").getAbsolutePath()); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } + + +} diff --git a/src/test/java/tutorialexercise/windows/WindowsTest.java b/src/test/java/tutorialexercise/windows/WindowsTest.java new file mode 100644 index 0000000..d755ebd --- /dev/null +++ b/src/test/java/tutorialexercise/windows/WindowsTest.java @@ -0,0 +1,30 @@ +package tutorialexercise.windows; + +import browser.BrowserGetter; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.support.PageFactory; +import tutorialsolution.pages.WindowsPage; + +import java.io.File; + +public class WindowsTest { + private WebDriver driver; + private BrowserGetter browserGetter = new BrowserGetter(); + private WindowsPage page; + + @BeforeEach + public void beforeEach() { + //initialize the Chrome browser here + driver = browserGetter.getChromeDriver(); + //initialize page object class + page = PageFactory.initElements(driver, WindowsPage.class); + driver.get(new File("src/main/resources/mainPage.html").getAbsolutePath()); + } + + @AfterEach + public void afterEach() { + driver.quit(); + } +} diff --git a/src/test/java/tutorialsolution/cookies/CookiesTest.java b/src/test/java/tutorialsolution/cookies/CookiesTest.java new file mode 100644 index 0000000..bb93c5d --- /dev/null +++ b/src/test/java/tutorialsolution/cookies/CookiesTest.java @@ -0,0 +1,82 @@ +package tutorialsolution.cookies; + +import browser.BrowserGetter; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.openqa.selenium.Cookie; +import org.openqa.selenium.WebDriver; + +import java.util.Date; +import java.util.HashSet; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; + +@TestInstance(PER_CLASS) +public class CookiesTest { + private final BrowserGetter browserGetter = new BrowserGetter(); + private WebDriver driver; + + @BeforeAll + public void beforeAll() { + //initialize the Chrome browser here + driver = browserGetter.getChromeDriver(); + driver.get("https://example.com"); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } + + @Test + public void workingWithCookies() { + //new Cookie object with only a name and value + Cookie firstCookie = new Cookie("firstCookieName", "firstCookieValue"); + //new Cookie with name and value, set on the root path of the domain, with a specified expiryDate in the future + Cookie secondCookie = new Cookie("secondCookieName", "secondCookieValue", "/", + new Date(System.currentTimeMillis() + 600000)); + + //before doing anything, clear all cookies in case any exist + driver.manage().deleteAllCookies(); + //check that there are no cookies indeed + assertEquals(0, driver.manage().getCookies().size()); + + //set first cookie: only name and value are specified + driver.manage().addCookie(firstCookie); + //check that the cookie was set properly + assertEquals(1, driver.manage().getCookies().size()); + assertEquals(firstCookie, driver.manage().getCookieNamed("firstCookieName")); + driver.navigate().refresh(); + + //set second cookie: name, value, path and expiry date were specified + driver.manage().addCookie(secondCookie); + assertEquals(2, driver.manage().getCookies().size()); + + //check that the expiry date of the cookie is in the future + int compareToResult = new Date(System.currentTimeMillis()).compareTo(driver.manage().getCookieNamed( + "secondCookieName").getExpiry()); + assertEquals(-1, compareToResult); + driver.navigate().refresh(); + + //check what cookies are set on the domain + Set expectedCookies = new HashSet<>(); + expectedCookies.add(firstCookie); + expectedCookies.add(secondCookie); + assertEquals(expectedCookies, driver.manage().getCookies()); + + //delete second cookie by name + driver.manage().deleteCookieNamed("secondCookieName"); + assertEquals(1, driver.manage().getCookies().size()); + Set expectedRemainingCookie = new HashSet<>(); + expectedRemainingCookie.add(firstCookie); + assertEquals(expectedRemainingCookie, driver.manage().getCookies()); + + //delete cookie by specifying the Cookie object used for creating the cookie + driver.manage().deleteCookie(firstCookie); + assertEquals(0, driver.manage().getCookies().size()); + } +} diff --git a/src/test/java/tutorialSolution/cssSelectorsSolution/CSSSelectorsTest.java b/src/test/java/tutorialsolution/cssselectorssolution/CSSSelectorsTest.java similarity index 90% rename from src/test/java/tutorialSolution/cssSelectorsSolution/CSSSelectorsTest.java rename to src/test/java/tutorialsolution/cssselectorssolution/CSSSelectorsTest.java index f49c087..55a5bac 100644 --- a/src/test/java/tutorialSolution/cssSelectorsSolution/CSSSelectorsTest.java +++ b/src/test/java/tutorialsolution/cssselectorssolution/CSSSelectorsTest.java @@ -1,4 +1,4 @@ -package tutorialSolution.cssSelectorsSolution; +package tutorialsolution.cssselectorssolution; import browser.BrowserGetter; import org.junit.jupiter.api.AfterAll; @@ -8,7 +8,7 @@ import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.PageFactory; -import tutorialSolution.pages.BasicPage; +import tutorialsolution.pages.BasicPage; import java.io.File; @@ -135,6 +135,8 @@ public void strategiesDemonstrated() { System.out.println(page.level2SiblingsWithDifferentTags.getText()); //level 2 with siblings of same tag as item ALPHA System.out.println(page.level2SiblingsSameTag.get(1).getText()); + //level 2 with siblings of same tag as item ALPHA but ALPHA has class + System.out.println("level2SiblingsSameTagAndAlphaHasClass" + page.level2SiblingsSameTagAndAlphaHasClass.getText()); //level 2 : dropdown System.out.println(page.dropdownOptions.get(2).getText()); //unordered list @@ -145,6 +147,8 @@ public void strategiesDemonstrated() { System.out.println(page.level3.getText()); //level 3, no siblings, ALPHA=BRAVO System.out.println(page.level3AlphaEqualsBravo.getText()); + //level 3, no siblings, ALPHA=BRAVO + System.out.println("level3AlphaEqualsBravoAlphaHasClass" + page.level3AlphaEqualsBravoAlphaHasClass.getText()); //level 3, siblings on BRAVO level: ALPHA=h3, BRAVO=div //level 3, BRAVO has siblings, all BRAVO's children have same tag as ALPHA System.out.println(page.level3BravoIdenticalSiblings.get(1).getText()); diff --git a/src/test/java/tutorialsolution/elementinteractionsolution/ElementInteractionTest.java b/src/test/java/tutorialsolution/elementinteractionsolution/ElementInteractionTest.java new file mode 100644 index 0000000..8276faa --- /dev/null +++ b/src/test/java/tutorialsolution/elementinteractionsolution/ElementInteractionTest.java @@ -0,0 +1,259 @@ +package tutorialsolution.elementinteractionsolution; + +import browser.BrowserGetter; +import org.junit.jupiter.api.*; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.Color; +import org.openqa.selenium.support.PageFactory; +import org.openqa.selenium.support.ui.Select; +import tutorialsolution.pages.WebElementInteractionPage; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS; + +@TestInstance(PER_CLASS) +public class ElementInteractionTest { + private final BrowserGetter browserGetter = new BrowserGetter(); + private WebElementInteractionPage page; + private WebDriver driver; + + @BeforeAll + public void beforeAll() { + //initialize the Chrome browser here + driver = browserGetter.getChromeDriver(); + //initialize page object class + page = PageFactory.initElements(driver, WebElementInteractionPage.class); + } + + @BeforeEach + public void beforeEach() { + driver.get(new File("src/main/resources/interactions.html").getAbsolutePath()); + } + + @AfterAll + public void afterAll() { + driver.quit(); + } + + @Test + public void click() { + //checking a checkbox + page.checkboxToClick.click(); + //unchecking the checkbox + page.checkboxToClick.click(); + //checking a radio button + page.radioButtonToClick.click(); + //clicking a button which triggers the display of a message + page.buttonToClick.click(); + //clicking on a link which causes a redirect to another page + page.linkToClick.click(); + } + + @Test + public void sendKeys() { + //type into an HTML element whose 'type' attribute is 'text' + page.textInput.sendKeys("coffee"); + assertEquals("coffee", page.textInput.getAttribute("value")); + //type into a