Skip to content

Automation testing web tokopedia using cucumber selenium java

Notifications You must be signed in to change notification settings

wisnuwm/automation-bdd-tokped

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 

Repository files navigation

automation-bdd-tokped

Automation Web Testing using Selenium Java, #BDD Testing using Cucumber Selenium Java

This project is for my portofolio

Hi:wave:, My name is Wisnu Munawar, I Love outdoor activity like hiking, camping etc:sunrise_over_mountains:, I studied informatics engineering at Indraprasta PGRI University:computer:

My LinkedIn : Click Here

My resume : Click Here

Guidance

  1. Create Maven project Eclipse

  1. add dependencies at pom.xml

<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>

	<dependencies>

		<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
		<dependency>
			<groupId>io.cucumber</groupId>
			<artifactId>cucumber-java</artifactId>
			<version>6.9.0</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.13.1</version>
			<scope>test</scope>
		</dependency>

		<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
		<dependency>
			<groupId>io.cucumber</groupId>
			<artifactId>cucumber-junit</artifactId>
			<version>6.9.0</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>3.141.59</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/net.masterthought/cucumber-reporting -->
		<dependency>
			<groupId>net.masterthought</groupId>
			<artifactId>cucumber-reporting</artifactId>
			<version>5.4.0</version>
		</dependency>


	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.22.0</version>
				<configuration>
					<testFailureIgnore>true</testFailureIgnore>
				</configuration>
			</plugin>
			<plugin>
				<groupId>net.masterthought</groupId>
				<artifactId>maven-cucumber-reporting</artifactId>
				<version>2.8.0</version>
				<executions>
					<execution>
						<id>execution</id>
						<phase>verify</phase>
						<goals>
							<goal>generate</goal>
						</goals>
						<configuration>
							<projectName>BDDTokped</projectName>
							<outputDirectory>${project.build.directory}/cucumber-report-html</outputDirectory>
							<cucumberOutput>${project.build.directory}/cucumber.json</cucumberOutput>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>

  1. Create folder drivers at src/test/resources and Add chromedriver.exe to src/test/resources/drivers

  2. Create folder Features at src/test/resources

  3. Create file login-kaskus.feature

  1. add code kaskus-login.feature

Feature: Feature test tokopedia login page
  User want to login successfully

  Scenario: Validate tokopedia login is working
    Given User open the browser
    And User on tokopedia login page
    When User enter valid email or phone number
    And Click Selanjutnya button
    And User enter valid password
    And Click Login button
    Then User navigated to verify with phone number or email

  1. Right click and Run as Cucumber feature

  2. Create folder StepDef at src/test/java and create java class BDD_Login_Tokped.java


package StepDefinition;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import io.cucumber.java.en.*;

public class BDD_Login_Tokped {

	WebDriver driver=null;

	@Given("User open the browser")
	public void user_open_the_browser() {
		System.out.println("Steps - User open the browser");
		
		//disable notif browser
		ChromeOptions opt = new ChromeOptions(); 
		opt.addArguments("--disable-notifications");
		
		//chromedriver setup
		System.setProperty("webdriver.chrome.driver", "C:/Users/wisnu/eclipse-workspace/BDDTokped/src/test/resources/drivers/chromedriver.exe");

		//open Chrome browser
		driver = new ChromeDriver(opt);

		//Maximize browser
		driver.manage().window().maximize();

	}

	@And("User on tokopedia login page")
	public void user_on_tokopedia_login_page() {
		System.out.println("Steps - User on tokopedia login page");

		//open tokped login page
		driver.get("https://www.tokopedia.com/login");
	}

	@When("User enter valid email or phone number")
	public void user_enter_valid_email_or_phone_number() {
		System.out.println("Steps - User enter valid email or phone number");

		//input email or phone number
		driver.findElement(By.id("email")).sendKeys("YourEmail/PhoneNumberHere");
	}

	@And("Click Selanjutnya button")
	public void click_selanjutnya_button() throws InterruptedException {
		System.out.println("Steps - Click Selanjutnya button");

		//click "selanjutnya"
		driver.findElement(By.xpath("/html/body/div[1]/div[3]/div/section[1]/form/button")).click();
		Thread.sleep(3000);
	}

	@And("User enter valid password")
	public void user_enter_valid_password() {
		System.out.println("Steps - User enter valid password");

		//input password
		driver.findElement(By.id("password")).click();
		driver.findElement(By.id("password")).clear();
		driver.findElement(By.id("password")).sendKeys("YourPasswordHere");
	}

	@And("Click Login button")
	public void click_login_button() {
		System.out.println("Steps - Click Login button");

		//click "masuk"
		driver.findElement(By.id("login-submit")).click();
	}

	@Then("User navigated to verify with phone number or email")
	public void user_navigated_to_verify_with_phone_number_or_email() throws InterruptedException {
		System.out.println("Steps - User navigated to verify with phone number or email");

		//user navigated to verify account
		driver.getPageSource().contains("Pilih Metode Verifikasi");
		Thread.sleep(3000);

		//close browser
		driver.close();
		driver.quit();
	}

}

  1. Run with cucumber feature again

  2. Create Folder Runner at src/test/java and create java class TestRunner.java


package runner;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
	
	@RunWith(Cucumber.class)
	@CucumberOptions(features="src/test/resources/Features",
	glue= {"StepDefinition"},
	plugin ={"json:target/cucumber.json"})

	public class TestRunner {
}

  1. Run TestRunnet.java as JUnit Test

  2. open command and enters "mvn clean" test will generate the Cucumber report file

  3. enters "mvn verify -DskipTests" will generate the cucumber-report-html based on the cucumber.json