Test site: https://www.daraz.com.bd/
The following instructions will help you navigate those testing pages. We will create some packages. At the package level, there is a list of classes where you can create methods, use methods for particular pages, and run and test the testing pages. We can also perform tests on the Daraz login Page by filling in fields in an automated way.
Test Some pages of Daraz website and the daraz login page the following tasks:
-
Set Environment i) pom.xml [dependencies set] ii) BrowserSetup [create a separate package ]
-
Page Object Model: create methods, using methods for separate page and create test cases of those pages a) Methods b) Utitilites [Page objects] c) TestCase [Particular Pages ]
-
Create Allure report
Create a Maven Project Set pom.xml file pom.xml file Code: Set Under Dependencies
<dependencies>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.6.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.3.0</version>
</dependency>
</dependencies>
</project>
Create Some class under Packages
One of the packages will hold BorwserSetup, in which we run the automation Code Inside BorwserSetup Class:
It will hold four drivers; use according to your preferences I found some difficulties running Chrome Browser, which is why I used Edge Driver to run my code.
package com.bd.Aug;
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.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import io.github.bonigarcia.wdm.WebDriverManager;
public class BrowserSetup {
private static String BrowserName = System.getProperty("browser", "Edge");
private static final ThreadLocal<WebDriver> DRIVER_LOCAL = new ThreadLocal<>();
public static WebDriver getDriver() {
return DRIVER_LOCAL.get();
}
public static void setDriver(WebDriver driver) {
BrowserSetup.DRIVER_LOCAL.set(driver);
}
public static WebDriver getBrowser(String BrowserName) {
switch (BrowserName.toLowerCase()) {
case "chrome":
ChromeOptions option1 = new ChromeOptions();
option1.addArguments("--remote-allow-origins=*");
WebDriverManager.chromedriver().setup();
return new ChromeDriver(option1);
case "edge":
EdgeOptions option2 = new EdgeOptions();
option2.addArguments("--remote-allow-origins=*");
WebDriverManager.edgedriver().setup();
return new EdgeDriver(option2);
case "firefox":
FirefoxOptions option3 = new FirefoxOptions();
option3.addArguments("--remote-allow-origins=*");
WebDriverManager.firefoxdriver().setup();
return new FirefoxDriver(option3);
default:
throw new RuntimeException("Browser Not found");
}
}
@BeforeSuite
public static synchronized void setBrowser() {
WebDriver webDriver = getBrowser(BrowserName);
webDriver.manage().window().maximize();
setDriver(webDriver);
}
@AfterSuite
public static synchronized void quitBrowser() {
getDriver().quit();
}
}
Create a package which include classes like method and other testing Page
method class includes methods of getElement, click, hover, getText, field fillup, visible wait element, click wait element, Press ok in the popup, press cancel in the popup, Scroll up, scroll down, copy all, paste, next field, take screenshot
package Utilities;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.qameta.allure.Allure;
import static com.bd.Aug.BrowserSetup.getDriver;
import java.io.ByteArrayInputStream;
import java.time.Duration;
public class Methods {
public WebElement getElement(By locator) {
//driver.findElement(By.xpath())
return getDriver().findElement(locator);//getDriver() = driver
}
public void clickElement(By locator) {
getElement(locator).click();
}
public void clickWaitElement(By locator) {
WebDriverWait wait = new WebDriverWait(getDriver(), Duration.ofSeconds(20));
//getElement(locator).click();
WebElement element= wait.until(ExpectedConditions.elementToBeClickable(locator));
element.click();
}
public void WaitElementVisible(By locator) {
WebDriverWait wait = new WebDriverWait(getDriver(), Duration.ofSeconds(20));
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}
public String gettext(By locator) {
return getElement(locator).getText();
}
// field entry
public void FieldValue(By locator,String text) {
getElement(locator).sendKeys(text);
}
public void Hover(By locator) {
Actions action = new Actions(getDriver());
//action.moveToElement(driver.findElement(locator)).perform();
action.moveToElement(getElement(locator)).perform();;
}
//alert or tost message ok and cancel
public void PressOk() {
//driver.switchTo().alert().accept();
getDriver().switchTo().alert().accept();
}
public void PressCancel() {
getDriver().switchTo().alert().dismiss();
}
public void ScrollUp() {
JavascriptExecutor js =(JavascriptExecutor)getDriver();
js.executeScript("window.scrollTo(document.body.scrollHeight,0)");
}
public void ScrollDown() {
JavascriptExecutor js = (JavascriptExecutor)getDriver();
js.executeScript("window.scrollTo(0,document.body.scrollHeight)");
}
public void CopyAll() {
Actions action = new Actions(getDriver());
action.keyDown(Keys.CONTROL);
action.sendKeys("a");
action.keyUp(Keys.CONTROL);
action.build().perform();
action.keyDown(Keys.CONTROL);
action.sendKeys("c");
action.keyUp(Keys.CONTROL);
action.build().perform();
}
public void Paste(){
Actions action = new Actions(getDriver());
action.keyDown(Keys.CONTROL);
action.sendKeys("v");
action.keyUp(Keys.CONTROL);
action.build().perform();
}
//you can use it multiple times back to back
public void NextField() {
Actions action = new Actions(getDriver());
action.sendKeys(Keys.TAB);
action.build().perform();;
}
//for allure report
public void takeScreenshot(String name) {
Allure.addAttachment(name, new ByteArrayInputStream(((TakesScreenshot)getDriver()).getScreenshotAs(OutputType.BYTES)));
}
}
Using those methods to create test page objects Page directory image:
------------------testing page image--------------
Page Objects Creation
package Utilities;
import org.openqa.selenium.By;
public class HelpCenterPage extends Methods{
public By HELPSUPPORTBUTTON = By.xpath("//span[contains(text(),'Help & Support')]");
public By HTITLE= By.xpath("//span[@class='helpText__foZwJBMm']");
public By HELPCENTERPAGE = By.xpath("//a[contains(text(),'Help Center')]");
public String HelpPagetitle = "Help Center";
public void GoHelpCenterPage() {
Hover(HELPSUPPORTBUTTON);
clickWaitElement(HELPCENTERPAGE);
WaitElementVisible(HELPCENTERPAGE);
}
}
Page directory image:
-------------Testing page image---------------
Page Objects Creation
package Utilities;
import org.openqa.selenium.By;
public class MotherAndBaby extends Methods{
public By MOTHERBABAY = By.xpath("//span[normalize-space()='Mother & Baby']");
public By KIDSTOY = By.xpath("(//span[contains(text(),'Kids Toys')])[1]");
public By ACTIONFIGURES = By.xpath("//img[@alt='Action Figures & Collectibles']");
public By CURRENTPAGETITLE = By.xpath("//title[contains(text(),'Action Figures - Buy Action Figures at Best Price ')]");
public String PAGETITLE = "Action Figures - Buy Action Figures at Best Price in Bangladesh | www.daraz.com.bd";
public void MotherAndBabyCategory() throws InterruptedException{
Hover(MOTHERBABAY);
Thread.sleep(2000);
Hover(KIDSTOY);
Thread.sleep(2000);
clickElement(ACTIONFIGURES);
takeScreenshot("Action Figure Page Overview");
}
}
Page directory image:
-------------Login testing page---------------
Page Objects Creation
package Utilities;
import org.openqa.selenium.By;
public class LoginPage extends Methods{
public By LONGINPAGE =By.xpath("//a[contains(text(),'Login')]");
public String loginPageTitle ="Daraz.com.bd: Online Shopping Bangladesh - Mobiles, Tablets, Home Appliances, TV, Audio & More";
public By USERNAMEFIELD = By.xpath("//input[@type ='text']");
public By PASSWORDFIELD = By.xpath("//input[@type ='password']");
public By LOGINBUTTON = By.xpath("//button[contains(text(),'LOGIN')]");
public void DoLoginPage(String email, String password) throws InterruptedException{
clickElement(LONGINPAGE);
Thread.sleep(5000);
FieldValue(USERNAMEFIELD, email);
FieldValue(PASSWORDFIELD, password);
clickElement(LOGINBUTTON);
}
}
Create Test Case Package Create Sepeate class for separate page
i) For Help Center page
package TestCases;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
import com.bd.Aug.BrowserSetup;
import Utilities.HelpCenterPage;
public class HelpCenterPageTest extends BrowserSetup {
HelpCenterPage helpcenter = new HelpCenterPage();
@Test(description = "Help Center Page Test")
public void HTest() throws InterruptedException{
getDriver().get("https://www.daraz.com.bd/");
Thread.sleep(2000);
helpcenter.GoHelpCenterPage();
Thread.sleep(2000);
assertEquals(getDriver().getTitle(), helpcenter.HelpPagetitle);
System.out.println(getDriver().getTitle());
helpcenter.takeScreenshot("Help Center Page");
}
}
ii) For Login page
package TestCases;
import org.testng.annotations.Test;
import com.bd.Aug.BrowserSetup;
import Utilities.LoginPage;
import io.qameta.allure.Description;
public class LoginPagetest extends BrowserSetup{
LoginPage loginpage = new LoginPage();
@Test
@Description ("Login Page Test")
public void loginSignUp() throws InterruptedException{
getDriver().get("https://www.daraz.com.bd/");
Thread.sleep(2000);
loginpage.DoLoginPage("[email protected]", "sakif@006");
Thread.sleep(2000);
//driver.getTitle() = that page title
System.out.println(getDriver().getTitle());
loginpage.takeScreenshot("Login Page Overview");
}
}
i) For Daraz menu check [Mother & Baby menu page]
package TestCases;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
import com.bd.Aug.BrowserSetup;
import Utilities.MotherAndBaby;
public class MotherAndBabyCategoryTest extends BrowserSetup{
MotherAndBaby motherandbaby = new MotherAndBaby();
@Test
public void TestMotherAndBabyCategory() throws InterruptedException{
getDriver().get("https://www.daraz.com.bd/");
Thread.sleep(2000);
motherandbaby.MotherAndBabyCategory();
assertEquals(getDriver().getTitle(), motherandbaby.PAGETITLE);
System.out.println(getDriver().getTitle());
}
}
To create an allure report, first set dependency in the pom.xml file. io.qameta.allure allure-testng 2.19.0 2. create another pom.xml file by running whole test togather file name would be testing.xml --Whole Testpackage convert testng
-- Creating test.xml file procedure --Test.xml file look like
-
then run the testing.xml file
-
then refresh the whole package and see a "allure-results" file created under Maven Dependencies -before refresh -after refresh
-
then write in terminal to clean previous files> ""allure generate ./allure-result --clean""
-
then write in terminal to create allure report> ""allure open ./allure-report""
-
terminal gives us http to show us an allure report file directory ---------6,7,8 terminal looklike-------------
-
Create some methods for allure report (like allure ScreenShot)
-
method add:
public void takeScreenshot(String name) {
Allure.addAttachment(name, new ByteArrayInputStream(((TakesScreenshot)getDriver()).getScreenshotAs(OutputType.BYTES)));
}