Giter Site home page Giter Site logo

java-selenium-demo's Introduction

java-selenium-exercise

Runs Selenium Web UI tests using Java, Gradle, WebDriverManager, and GitHub actions

Part 0: Setting up the Java Project

  • File -> New Project
  • Choose project name, directory
  • Under dependencies section in build.gradle add:
    •   testImplementation 'junit:junit:4.13.2'
        implementation 'org.seleniumhq.selenium:selenium-java:4.4.0'
    • Click on "Load Gradle Changes" (Shift + Command + I)

Part 1: Installing ChromeDriver Manually

  • We will need to create an instance of a web driver; we can use ChromeDriver for now
  • Create a test class file TestImplementation.java in /src/test/java
    •   import org.junit.*;
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.chrome.ChromeDriver;
        
        import static org.hamcrest.CoreMatchers.containsString;
        import static org.hamcrest.MatcherAssert.assertThat;
        
        public class TestImplementation {
            private WebDriver driver;
            @BeforeClass
            public static void setupWebdriverChromeDriver() {
                System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/src/main/resources/chromedriver");
            }
        
            @Before
            public void setup() {
                driver = new ChromeDriver();
            }
        
            @After
            public void teardown() {
                if (driver != null) {
                    driver.quit();
                }
            }
        
            @Test
            public void verifyGitHubTitle() {
                driver.get("https://www.github.com");
                assertThat(driver.getTitle(), containsString("GitHub"));
            }
        }
  • Code will work but you'll get SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
  • Run the test
    • You can do this via IntelliJ or via CLI: gradle test
  • You can view HTML reports at the command line via open build/reports/tests/test/index.html

Part 2: Using WebDriverManager

  • This time we'll use [[WebDriverManager]] to automate the browser setup
  • After Part 1, add the following build.gradle dependency:
    • testImplementation group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '3.3.0'
  • In the test file, change @BeforeClass and @Before appropriately:
    •   // ...
        import org.openqa.selenium.chrome.ChromeDriver;
        // import org.openqa.selenium.firefox.FirefoxDriver;
        // ...
        
        @BeforeClass
        public static void setupWebdriverBinary() {
        WebDriverManager.chromedriver().setup();
        // WebDriverManager.firefoxdriver().setup();
        }
        @Before
        public void setup() {
        driver = new ChromeDriver();
        // driver = new FirefoxDriver();
        }
  • To prove we're not using the local ChromeDriver we previously added in Part 1, let's remove the chromedriver file and re-run our tests:
    • rm src/main/resources/chromedriver
  • See using-webdriver branch on https://github.com/ErnieAtLYD/java-selenium-exercise

Resources

Part 3: Moving all of this to GitHub Actions

  • Let's try having a CI/CD such as GitHub Actions do the work for us
    • We don't have to worry about ChromeDriver executables now that WebDriverManager is handling this
    • But we DO have to assume that the Linux machine that GitHub actions will use won't have Chrome installed. We also have to figure out how to do this all in headless mode
  • Go into headless mode
    • add import org.openqa.selenium.chrome.ChromeOptions;
    • Change setup() to the following:
      •   @Before
          public void setup() {
              ChromeOptions options = new ChromeOptions();
              options.addArguments("--no-sandbox");
              options.addArguments("--disable-dev-shm-usage");
              options.addArguments("--headless");
              driver = new ChromeDriver(options);
              // driver = new FirefoxDriver();
          }
  • Write a bash script called InstallChrome.sh that will install
    •   #!/bin/bash
        set -ex
        wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
        sudo apt install ./google-chrome-stable_current_amd64.deb
  • Create the GitHub action workflow
    • Create .github/workflows/gradle.yml:
      •   name: Selenium Java CI
          
          on: [push]
          
          jobs:
          build:
              runs-on: ubuntu-latest # Using linux machine
          
              steps:
              - uses: actions/checkout@v2 # Checkout the code
              - name: Set up JDK 1.8
                  uses: actions/setup-java@v1 # Setup JAVA
                  with:
                  java-version: 1.8
              - name: Install Google Chrome # Using shell script to install Google Chrome
                  run: |
                  chmod +x ./scripts/InstallChrome.sh
                      ./scripts/InstallChrome.sh
              - name: Grant execute permission for gradlew
                  run: chmod +x gradlew # give permission to Gradle to run the commands
              - name: Build with Gradle
                  run: ./gradlew test --info # Run our tests using Gradle
          
    • Submit a pull request
  • See using-github-actions branch on https://github.com/ErnieAtLYD/java-selenium-exercise

Resources

java-selenium-demo's People

Contributors

ernieatlyd avatar

Watchers

 avatar  avatar

Forkers

strategio-tech

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.