diff --git a/src/test/java/BaseTest.java b/src/test/java/BaseTest.java index ceb3f82..295a531 100644 --- a/src/test/java/BaseTest.java +++ b/src/test/java/BaseTest.java @@ -1,11 +1,84 @@ import io.github.bonigarcia.wdm.WebDriverManager; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; +import org.openqa.selenium.edge.EdgeDriver; +import org.openqa.selenium.edge.EdgeOptions; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.remote.RemoteWebDriver; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeSuite; +import org.testng.annotations.Parameters; + +import java.net.MalformedURLException; +import java.net.URL; +import java.time.Duration; +import java.util.HashMap; public class BaseTest { + public WebDriver driver = null; + public String url = null; + private static final ThreadLocal threadDriver = new ThreadLocal<>(); + protected void navigateToPage() { + driver.get(url); + } @BeforeSuite - static void setupClass() { - WebDriverManager.chromedriver().setup(); + public static WebDriver getDriver() { + return threadDriver.get(); + } + @BeforeMethod + @Parameters({"BaseUrl"}) + public void setupBrowser(String BaseURL) throws MalformedURLException { + threadDriver.set(pickBrowser(System.getProperty("browser"))); + getDriver().manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); + url = BaseURL; + navigateToPage(); + } + @AfterMethod + public void tearDown() { + threadDriver.get().close(); + threadDriver.remove(); + } + public WebDriver pickBrowser(String browser) throws MalformedURLException { + DesiredCapabilities caps = new DesiredCapabilities(); + String gridURL = "https://10.0.0.206:4444/"; + switch (browser) { + case "MicroSoftEdge": + WebDriverManager.edgedriver().setup(); + EdgeOptions edgeOptions = new EdgeOptions(); + edgeOptions.addArguments("--remote-allow-origins=*"); + return driver = new EdgeDriver(); + case "cloud": + return LambdaTest(); + + default: + WebDriverManager.chromedriver().setup(); + ChromeOptions chromeOptions = new ChromeOptions(); + chromeOptions.addArguments("--remote-allow-origins=*"); + return driver = new ChromeDriver(chromeOptions); + } + } + + public RemoteWebDriver LambdaTest() throws MalformedURLException { + String HubURL = "https://hub.lambdatest.com/wd/hub"; + DesiredCapabilities capabilities = new DesiredCapabilities(); + + ChromeOptions browserOptions = new ChromeOptions(); + browserOptions.setPlatformName("Windows 10"); + browserOptions.setBrowserVersion("122.0"); + HashMap ltOptions = new HashMap(); + ltOptions.put("username", "sadokha.david"); + ltOptions.put("accessKey", "poTHwxG4slaMyczhUE8kIvKa7zJoi2kZtvolIUsU8BlPTOt2wv"); + ltOptions.put("build", "TEST1"); + ltOptions.put("project", "Untitled"); + ltOptions.put("name", "TEST1Lambda"); + ltOptions.put("w3c", true); + ltOptions.put("plugin", "java-java"); + browserOptions.setCapability("LT:Options", ltOptions); + + return new RemoteWebDriver(new URL(HubURL), capabilities); + } } } \ No newline at end of file diff --git a/src/test/java/Basepage/BasePage.java b/src/test/java/Basepage/BasePage.java new file mode 100644 index 0000000..2f188e9 --- /dev/null +++ b/src/test/java/Basepage/BasePage.java @@ -0,0 +1,4 @@ +package Basepage; + +public class BasePage { +} diff --git a/src/test/java/Basepage/HomePage.java b/src/test/java/Basepage/HomePage.java new file mode 100644 index 0000000..0e2172b --- /dev/null +++ b/src/test/java/Basepage/HomePage.java @@ -0,0 +1,17 @@ +package Basepage; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; + +public class HomePage extends BasePage { + @FindBy(css = "a.view-profile") + WebElement profileIcon; + public HomePage(WebDriver givenDriver) { + super(givenDriver); + } + public HomePage clickProfileIcon() { + click(profileIcon); + return this; + } +} diff --git a/src/test/java/Basepage/ProfilePage.java b/src/test/java/Basepage/ProfilePage.java new file mode 100644 index 0000000..7255ebe --- /dev/null +++ b/src/test/java/Basepage/ProfilePage.java @@ -0,0 +1,23 @@ +package Basepage; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; + +public class ProfilePage extends BasePage { + public ProfilePage(WebDriver givenDriver) { + super(givenDriver); + } + @FindBy(css = "[data-testid='theme-card-violet']") + WebElement violetTheme; + @FindBy(css = "[data-testid='theme-card-violet'][class='theme selected']") + WebElement selectedVioletTheme; + public boolean isVioletThemeSelected() { + findElement(selectedVioletTheme); + return selectedVioletTheme.isDisplayed(); + } + public ProfilePage chooseVioletTheme() { + click(violetTheme); + return this; + } +} diff --git a/src/test/java/LoginTests.java b/src/test/java/LoginTests.java index 8df7dcb..03990ce 100644 --- a/src/test/java/LoginTests.java +++ b/src/test/java/LoginTests.java @@ -1,25 +1,60 @@ import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; +import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.support.FindBy; import org.testng.Assert; import org.testng.annotations.Test; +import java.net.MalformedURLException; import java.time.Duration; +import java.util.HashMap; public class LoginTests extends BaseTest { + + + @Test + public void loginWithCorrectCredentials1() { + + LoginPage loginPage = new LoginPage(getDriver()); + HomePage homePage = new HomePage(getDriver()); + + loginPage.provideEmail(); + .providePassword(); + .clickSubmit(); + + Assert.assertTrue(homePage.getUserAvatarIcon().isDisplayed()); + } @Test public void loginEmptyEmailPassword() { -// Added ChromeOptions argument below to fix websocket error - ChromeOptions options = new ChromeOptions(); - options.addArguments("--remote-allow-origins=*"); + LoginPage loginPage = new LoginPage(getDriver()); - WebDriver driver = new ChromeDriver(options); - driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); + loginPage.provideEmail(); + .providePassword(); + .clickSubmit(); - String url = "https://qa.koel.app/"; - driver.get(url); - Assert.assertEquals(driver.getCurrentUrl(), url); - driver.quit(); + homePage.clickProfileIcon(); + profilePage.chooseVioletTheme(); + Assert.assertTrue(profilePage.isVioletThemeSelected()); + } + @FindBy(css = "input[type='email']") + WebElement emailField; + @FindBy(css = "input[type='password']") + WebElement passwordField; + @FindBy("button[type='submit']") + WebDriver submitButton; + public void loginPage(WebDriver givenDriver) { + super(givenDriver); + } + public loginPage provideEmail(String email) { + emailField.sendKeys(email); + return this; } + public loginPage clickSubmit() { + click(submitButton); + return this; + } + } diff --git a/src/test/resources/TestNG.xml b/src/test/resources/TestNG.xml index 8a34058..4dc1219 100644 --- a/src/test/resources/TestNG.xml +++ b/src/test/resources/TestNG.xml @@ -1,9 +1,9 @@ - - - + + +