Cucumber - Spring boot - Automation Test - WebDriver - Selenium
Ở đây chúng tôi hướng dẫn bạn code demo, còn lại mời các nạn nhân của java automation test junit selenium đọc code và chạy test thử để biết thêm chi tiết. Đọc đến đây chắc là ngứa rồi =)). Mời vô đọc code nhé. Tìm được đến đây thì chắc không cần xem khái niệm nữa đâu . kkk. À chúng tôi cũng không đẩy code lên git Để cho ai cần thì copy cho nó thích =))
Step1 : Create a project java with : jdk8 , maven 3.9.5
Step2 : add dependency to pom.xml or gradle file: go to https://mvnrepository.com/ search by keyseach and add to file pom.xml or gradle file: spring-boot-starter-test lombok webdrivermanager junit-platform-suite cucumber-junit-platform-engine cucumber-spring cucumber-java junit-jupiter-api junit-jupiter-engine selenium-java spring-boot-configuration-processor spring-boot-starter-actuator spring-boot-starter-web
Step 3 : create class and test file wwith structure
Step 4 : add content to class WebDriverFactory: package org.automation.webdriver; import io.github.bonigarcia.wdm.WebDriverManager; import lombok.extern.slf4j.Slf4j; 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.Browser; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;
import java.util.List; import java.util.Random; @Component @Slf4j public class WebDriverFactory { public static final ThreadLocal<WebDriver> THREAD_LOCAL_DRIVER = new ThreadLocal<>(); private static final Random random = new Random(); private static List<String> BROWSER_LIST; private static boolean randomizeBrowser; private static String defaultBrowser; public static WebDriver getDriver() { if (THREAD_LOCAL_DRIVER.get() != null) { return THREAD_LOCAL_DRIVER.get(); } else { log.error("webdriver null =))"); throw new RuntimeException("webdriver null =))"); } } public static void cleanUpDriver() { WebDriverFactory.quitDriver(); WebDriverFactory.removeDriver(); } public static void quitDriver() { if (THREAD_LOCAL_DRIVER.get() != null) { THREAD_LOCAL_DRIVER.get().quit(); } } public static void removeDriver() { if (THREAD_LOCAL_DRIVER.get() != null) { THREAD_LOCAL_DRIVER.remove(); } } public static void createDriver() { String browserType = defaultBrowser; if (randomizeBrowser) { int randomItem = random.nextInt((BROWSER_LIST.size())); browserType = BROWSER_LIST.get(randomItem); } log.info("using browser type {}", browserType); if (Browser.CHROME.is(browserType)) { THREAD_LOCAL_DRIVER.set(createLocalChromeDriver()); } else if (Browser.EDGE.is(browserType)) { THREAD_LOCAL_DRIVER.set(createLocalMSEdgeDriver()); } else { log.error("unknown type browser"); throw new RuntimeException("unknown type browser"); } } public static WebDriver createLocalChromeDriver() { WebDriverManager.chromedriver().setup(); ChromeOptions chromeOptions = new ChromeOptions(); WebDriver webDriver = new ChromeDriver(chromeOptions); setBasicDriverProperties(webDriver); return webDriver; } public static WebDriver createLocalMSEdgeDriver() { WebDriverManager.edgedriver().setup(); EdgeOptions edgeOptions = new EdgeOptions(); WebDriver webDriver = new EdgeDriver(edgeOptions); setBasicDriverProperties(webDriver); return webDriver; } private static void setBasicDriverProperties(WebDriver webDriver) { webDriver.manage().window().maximize(); } @Value("#{'${test.browser,list}'.split(',')}") public void setBrowserList(List<String> browserList) { BROWSER_LIST = browserList; } @Value("${test.browser.randomize}") public void setRandomizeBrowser(boolean randomizeBrowser) { WebDriverFactory.randomizeBrowser = randomizeBrowser; } @Value("${test.browser.default}") public void setDefaultBrowser(String defaultBrowser) { WebDriverFactory.defaultBrowser = defaultBrowser; } }
Step 5 : add content to Application class: package org.automation; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@Slf4j @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Step 6 : add content to application.properties file : logging.level.org.springframework = INFO test.browser,list = chrome,firefox,safari,MicrosoftEdge test.browser.randomize = false test.browser.default = MicrosoftEdge
Step 7 : add content to VibloTests class : package org.automation.steps.ui; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; import org.automation.webdriver.WebDriverFactory; import org.openqa.selenium.By;
import static org.junit.jupiter.api.Assertions.assertEquals; public class VibloTests extends WebDriverFactory { @When("I open page {string}") public void iOpenPage(String pageUrl) { getDriver().get(pageUrl); } @Then("I verify Viblo name {string}") public void iVerifyVibloName(String expectedChannelName) { String actualChannelName = getDriver().findElement(By.className("article-content__title")).getText(); assertEquals(actualChannelName,expectedChannelName); } }
Step 8 : add content to VibloTests class : package org.automation.steps.ui; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; import org.automation.webdriver.WebDriverFactory; import org.openqa.selenium.By;
import static org.junit.jupiter.api.Assertions.assertEquals; public class YouTubeTests extends WebDriverFactory { @When("I go to page {string}") public void iGoToPage(String pageUrl) { getDriver().get(pageUrl); } @Then("I verify YouTube channel name {string}") public void iVerifyYouTubeChannelName(String expectedChannelName) { String actualChannelName = getDriver().findElement(By.id("channel-name")).getText(); assertEquals(actualChannelName,expectedChannelName); } }
Step 9 : add content to CucumberHooks class : package org.automation.steps; import io.cucumber.java.After; import io.cucumber.java.Before; import org.automation.webdriver.WebDriverFactory;
public class CucumberHooks { @Before("@ui") public void beforeEach(){ WebDriverFactory.createDriver(); } @After("@ui") public void afterEach(){ WebDriverFactory.cleanUpDriver(); } }
Step 10 : add content to CucumberSpringConfiguration class : package org.automation.steps; import io.cucumber.spring.CucumberContextConfiguration; import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @CucumberContextConfiguration public class CucumberSpringConfiguration { }
Step 11: add content to CucumberSpringConfiguration class :
package org.automation;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;
@Suite @IncludeEngines("cucumber") @SelectClasspathResource("featurefiles") public class BaseCucumberTests { }
Step 12: add content to Viblo.feature file :
@ui
Feature: Viblo Test
Scenario: Verify Viblo name When I open page "https://viblo.asia/p/keycloack-loadbalancer-springboot-angular-L4x5xV91ZBM" Then I verify Viblo name "Keycloack - loadbalancer - springboot - angular"
Step 13: add content to YouTube.feature file:
@ui
Feature: YouTube Test
Scenario: Verify YouTube Channel name
When I go to page "https://www.youtube.com/channel/UCH0L0UYACGQTXLXaXp5k_yA"
Then I verify YouTube channel name "Cường Nguyễn"
Step 14: add content to junit-platform.properties file:
cucumber.glue=org.automation.steps
cucumber.plugin=pretty, junit:target/cucumber-report/Cucumber.xml
cucumber.execution.parallel.enabled=true
cucumber.execution.parallel.config.strategy=fixed
cucumber.execution.parallel.config.fixed.parallelism =5
cucumber.publish.enable=true
cucumber.junit-platform.naming-strategy=long
Step 15 : test in file .future