How to run Selenium tests with GitHub Actions

Last Updated On

HOME

This tutorial explains the steps to create a GitHub Action for the Selenium tests and execute the tests in that workflow.

Table of Contents

  1. Why GitHub?
  2. Important points
  3. Implementation Steps
    1. Create GitHub Actions and Workflows
    2. Select the type of Actions
    3. Generation of Sample pipeline
    4. Commit the changes
    5. Verify that the workflow is running

Why GitHub?

The flexible aspects of Selenium WebDrivers and GitHub Actions enable users to create powerful, fast, and efficient automated testing workflows in CI/CD environments.

CI/CD pipelines have contributed to the success of the DevOps cycle in all software development projects. This is a holistic process that bridges development and operations. Continuous integration helps development teams deploy code efficiently, and continuous delivery automates code deployment.

Important points

1. We don’t need to think about the Chromedriver.exe we are using WebDriver Manager to handle that instead of adding it to our project like the following:

     WebDriverManager.chromedriver().setup();
    

    From Selenium 4.6.0 onwards, we don’t need to add WebDriverManager dependency explicitly to the project, because Selenium has added an inbuilt tool to handle the drivers. 

    2. The Selenium tests need to run in the headless mode. As we are using Chrome browser, use the below code:

              ChromeOptions options = new ChromeOptions();
              options.addArguments("--no-sandbox");
              options.addArguments("--disable-dev-shm-usage");
              options.addArguments("--headless");
    	      driver = new ChromeDriver(options);
    

    3. Install Chrome browser in ubuntu. Use the below code:

        - uses: browser-actions/setup-chrome@latest
        - run: chrome --version
    
    

    Implementation Steps

    Step 1 – Create GitHub Actions and Workflows

    I have a repository available in GitHub – Selenium-TestNG as shown in the below image. Go to the “Actions” tab.  Click on the “Actions” tab.

    Step 2 – Select the type of Actions

    You will see that GitHub recommends Actions depending on the project. In our case, it is recommending actions suitable for a Java project. I have selected the “Java with Maven” option as my project is built in Maven.

    Step 3 – Generation of Sample pipeline

    If you choose an existing option, it will automatically generate a .yaml for the project as shown below.

    We will replace the current workflow with the following yml file as shown below:

    name: Selenium with TestNG - CI
    
    on:
      push:
        branches: [ "main" ]
      pull_request:
        branches: [ "main" ]
    
    jobs:
      build:
    
        runs-on: ubuntu-latest
    
        steps:
        - uses: actions/checkout@v3
        - name: Set up JDK 11
          uses: actions/setup-java@v3
          with:
            java-version: '11'
            distribution: 'temurin'
            cache: maven
        
        - uses: browser-actions/setup-chrome@latest
        - run: chrome --version
        
        - name: Build with Maven
          run: mvn -B clean test
    

    The workflow file usually consists of the following attributes:

    name: Java CI with Maven Name of workflow: This is an optional attribute.The name of the workflow is denoted by the keyword “name”. If you wish to include multiple CI jobs in your project, it is recommended to name each one appropriately.
    on: [push] :
    On attribute – This attribute is mandatory and specifies the trigger for this workflow. There are several actions you can include such as push, pull request, release, etc. for more information on the events, check the official guide here.
    jobs: Under the jobs attribute, groups together all the jobs that run in the workflow.
    runs-on: ubuntu-latest Configures the job to run on the latest version of an Ubuntu Linux runner. This means that the job will execute on a fresh virtual machine hosted by GitHub
    steps: Under this option, the actions used by the particular workflow, and the multiple jobs that would run under the workflow are mentioned. These jobs run in parallel. If you want to run the jobs in the order in the CI pipelines, you must add the “needs” attribute. It is crucial to ensure that every workflow has at least one job. In our sample workflow, we only have one job as shown in image above.
    uses: actions/checkout@v3
    The uses keyword specifies that this step will run v3 of the actions/checkout action. This is an action that checks out your repository onto the runner, allowing you to run scripts or other actions against your code (such as build and test tools). You should use the checkout action any time your workflow will run against the repository’s code.
    uses: actions/setup-java@v3
    with:
    java-version: ’11’
    This step uses the actions/setup-java@v3 action to install the specified version of the Java (this example uses v11).

    Step 4 – Commit the changes

    After the changes, hit the “Start Commit” button.

    This will give the option to add a description for the commit. It will also enable the user to commit either to the main branch or commit to any other branch that exists in the project. Click on the “Commit new file” button to set up the workflow file.

    Step 5 – Verify that the workflow is running

    Next, head over to the “Actions” tab, and you will see your YAML workflow file present under the tab. The yellow sign represents that the job is in the queue.

    In Progress – When the job starts building and running, you will see the status change from “Queued” to “in progress”.

    Passed – If the build is successful, you will see a green tick mark. 

    Click on the workflow and the below screen is displayed. It shows the status of the run of the workflow, the total time taken to run the workflow, and the name of the .yml file.

    Below are all the steps of the workflow.

    Failed – In case the build has failed, then there will be a red cross mark.

    To learn more about Chrome installation, please refer to this tutorial – browser-actions/setup-chrome.

    The complete code can be found here on GitHub – vibssingh/Selenium-TestNG.

    Congratulations! We just created our CI workflow for running our Selenium test cases.

    How to copy code from another version control to GitHub – GitHub Importer

    HOME

    What is GitHub Importer?

    GitHub Importer is a tool that quickly imports source code repositories, including commits and revision history, from Subversion, Mercurial, Team Foundation Version Control (TFVC), or another Git repository to GitHub for you.

    During an import, depending on the version control system you’re importing from, you can authenticate with your remote repository, update commit author attribution, and import repositories with large files (or remove large files if you don’t want to use Git Large File Storage).

    Code Transfer from GitLab to GitHub

    Implementation Steps

    Step 1 – n the upper-right corner of any page, click +, and then click Import repository.

    Step 2 – Under “Your old repository’s clone URL”, type the URL of the project you want to import. Here, I have mentioned the url of a project in GitLab.

    Step 3 – Choose your personal account or an organization to own the repository, then type a name for the repository on GitHub.

    Step 4 – Specify whether the new repository should be public or private. I have selected the public option.

    Step 5 – Review the information you entered, then click Begin import.

    Step 6 – If your old project requires credentials, type your login information for that project, then click Submit. If SAML SSO or 2FA are enabled for your user account on the old project, enter a personal access token with repository read permissions in the “Password” field instead of your password.

    We can see that this project is now imported to GitHub.

    Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

    What is pull request in GitHub?

    HOME

    What is Pull Request?

    Pull requests let others know about changes you’ve pushed to a branch of your repository on GitHub. When a pull request is opened, you can discuss and review potential changes with collaborators and add follow-up commits before the changes are merged into the base branch.

    Prerequisite:

    GIT is installed on the machine

    Valid account on GitHub

    Implementation Steps

    If you want to create a new branch for your pull request and do not have write permissions to the repository, you can fork the repository first. 

    1. Fork the repository

    By clicking the fork button at the top of the page, you can fork the repository. This will make a copy of the entire repository in your account. To know more about Forking, please refer to this tutorial – How to Fork a GitHub repository.

    2. Clone the repository

    Once the repository has been added to your account, you can clone it to your machine to work with it locally.

    Clone by clicking the clone button and copying the link as shown below.


    Open the terminal (GitBash) and run the following command. It will clone the repository locally.

    $ git clone https://github.com/vibssingh/GitHub-Demo.git
    

    Now we have set up a copy of the master branch from the main online project repository.

    We need to go to that cloned directory by running this command:

    $ cd GitHub-Demo
    

    3. Create a new branch

    When working with repositories, whether it’s a small project or contributing to the work of a group, it’s the best practice to create a new branch.

    The branch name should be short and reflect the work we are doing.

    Now, use the below command to create a branch:

    $ git checkout -B first_merge
    
    

    4. Make changes and commit the changes

    Make essential changes to the project and save it.

    Then execute git status, and you’ll see the changes.

    $ git status
    

    Add those changes to the branch you just created using the below command:

    $ git add .
    

    Now commit those changes using the git commit command:

    $ git commit -m "First Change by Vibha"
    

    5. Push changes to GitHub

    In order to push the changes to GitHub, we need to identify the remote’s name.

    $ git remote
    

    For this repository, the remote’s name is “origin”.

    After identifying the remote’s name we can safely push those changes to GitHub.

    $ git push origin first_merge
    
    

    6. Create pull request

    Go to your repository on GitHub, and you’ll see a button “Compare & pull request” click it.

    Please provide the necessary details on what you’ve done (You can reference issues using “#”). Now submit the pull request.

    7. Update the Local Repository

    Before submitting any pull requests to the original repository, you must first sync your repository with it.

    While working on a project with other contributors, keep your local repository up to date with the project because you don’t want to make a pull request for code that will automatically cause conflicts (though in collaborative code projects, conflicts are bound to occur). You’ll need to sync changes to keep your local copy of the code base up to date.

    To update/sync those changes to your master branch, follow these steps:

    1. First, check which branch you are in.

    $ git remote
    

    It’ll list all branches and indicates the current or active branch in green.

    2. Switch to the master branch

    git checkout main
    

    3. Add the original repository as an upstream repository

    In order to pull the changes from the original repository into your forked version, you need to add the original Git repository as an upstream repository.

    git remote add upstream https://github.com/original-owner-username/original-repository.git
    

    4. Fetch the repository

    Fetch all the changes from the original repository. Commits to the original repository will be stored in a local branch called upstream/master.

    $ git fetch upstream
    

    5. Merge the changes

    Merge the changes from the upstream/master into your local master branch. This will bring your fork’s master branch into sync with the upstream repository without losing your local changes.

    $ git merge upstream/main
    

    6. Push changes to GitHub

    At this point, your local branch is synced to the original repository’s master branch. If you want to update the GitHub repository, you need to push your changes.

    $ git push origin main
    

    Congratulations! You’ve made your first pull request.

    How to Clone a project from GitHub using Eclipse

    Last Updated On

    HOME

    In this tutorial, we will clone a project from GitHub and import it in Eclipse.

    Table Of Contents

    1. What is GitHub?
    2. Implementation Steps
      1. Clone the Project
      2. Import the project in Eclipse

    What is GitHub?

    GitHub is a for-profit organization that provides cloud-based Git repository hosting. It simplifies the use of Git for version control and collaboration by individuals and teams.
    The GitHub interface is simple enough that even inexperienced coders can use it. Without GitHub, using Git generally necessitates a bit more technical know-how and command-line proficiency.

    Furthermore, anyone can sign up for and host a public code repository for free, making GitHub particularly popular with open-source projects.

    In this tutorial, I will explain how we can clone a project from GitLab in Eclipse.

    Implementation Steps

    Clone the Project

    Step 1 – Go to GitHub and select the project which you want to clone. Click on the green colour “Code” button, then copy the hyperlink as shown in the image. You can either Clone with HTTPS, SSH, or GitHub CLI.

    Import the project in Eclipse

    Step 2 – Open Eclipse and go to File > Import in eclipse as shown in the image.

    Step 3 – A window will pop up in which select Git Folder. Under the Git folder, select the option – Projects from Git (with smart import) as shown in the image.

    Click on the NEXT button.

    Step 4 – A new window will pop up in which select the option – Clone URI as shown in the image.

    Click on the NEXT button.

    Step 5 – Another window will pop up in which you have to paste the GitHub Repository URL and also GitHub User ID and Password and click on the “Next” button.

    URI – This is the URL that we have cloned from GitHub in Step 1.
    Host – github.com
    Repository path – Path of the project in GitHub (This is auto-populated after entering URI)

    Authentication
    User – Username of GitHub
    Password – Password of GitHub

    Step 6 – Select main and selectWhen fetching a commit, also fetch its tags.

    Click on the “Next” button.

    Step 7 –  Select the Folder directory in which you want to import the repository.

    Click on the Next button.

    Step 8 – Select the Import Source, this is auto-generated. This wizard analyzes the content of the folder to find the project and import them into the IDE.

    Click on the Finish button.

    Step 9 – We have successfully imported the GitHub Repository as shown in the below image.

    Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

    How to Fork a GitHub Repository

    HOME

    A fork is a new repository that shares the original “upstream” repository’s code and visibility settings. Forks are frequently used in open-source projects or when a user does not have write access to the upstream repository to iterate on ideas or changes before they are proposed back to the upstream repository.

    What is GitHub Repository Forking?

    Assume you enjoy working on a specific framework or library. You discover a way to improve the functionality of this framework yourself one day.

    The source code is available as a public repository on GitHub, so you can fork it to make a local copy.

    Once you have a local copy of the code, you can make the necessary changes and request that the community review them.

    Following a review of your code changes, the community may approve them or request additional changes. They are most likely to accept your code changes after they have been approved.

    Prerequisites:

     Set up Git and authentication with GitHub.com from Git

    How to Fork a Repo in GitHub

    Step 1 – On GitHub.com, navigate to the octocat/Spoon-Knife repository.

    Step 2 – In the top-right corner of the page, click Fork.

    Step 3 – Select an owner for the forked repository. Here, the owner is vibssingh (as this is my username that is used to login).

    Step 4 – By default, forks are named the same as their upstream repositories. You can change the name of the fork to distinguish it further.

    Optionally, add a description of your fork.

    Here, the new name is GitHub-Demo.

    Step 5 – Choose whether to copy only the default branch or all branches to the new fork. For many forking scenarios, such as contributing to open-source projects, you only need to copy the default branch. By default, only the default branch is copied.

    Step 6 – Click Create fork button.

    This image shows that the GitHub repo is forked and present in your personal GitHub workspace.

    We have successfully forked a repository in GitHub. I hope you have enjoyed this tutorial. Happy Learning!!

    How to delete a repository on GitHub

    HOME

    The previous tutorial explained the steps to create a new repository in GitHub. In this tutorial, I will explain how we can delete n existing repository in GitHub.

    It is important to note that you can delete any repository or fork if you’re either an organization owner or have admin permissions for the repository or fork. Deleting a forked repository does not delete the upstream repository.

    • Deleting a repository will permanently delete release attachments and team permissions. This action cannot be undone.
    • Deleting a private repository will delete all forks of the repository

    Implementation Steps

    Step 1 – Login to GitHub using your username and password.

    Step 2 – Navigate to the main page of the repository. Under your repository name, click “Settings“.

    Step 3 – Scroll down and, under “Danger Zone“, click the “Delete this repository” button.

    Step 4 – To verify that you’re deleting the correct repository, type the name of the repository you want to delete.

    Click the “I understand the consequences, delete this repository” button.

    Step 5 – You need to re-login to confirm your access.

    Congratulations!!. We have just deleted a repository in GitHub.

    GitHub Tutorials

    HOME

    GitHub is a web-based version control system and collaboration platform for developers. It is the centre around which all things involving git revolve. GitHub allows software developers and engineers to create remote, public-facing repositories on the cloud for free. Once you’ve set up a repository on GitHub, you can copy it to your device, add and modify files locally, and then “push” your changes back to the repository where your changes are displayed for the public.

    Chapter 1 How to create a new repository on GitHub
    Chapter 2 How to delete a repository on GitHub
    Chapter 3 How to Fork a GitHub repository
    Chapter 4 How to clone a project from GitHub using Eclipse
    Chapter 5 How to copy code from another version control to GitHub – GitHub Importer
    Chapter 6 What is pull request in GitHub?
    Chapter 7 How to run Selenium tests with GitHub Actions
    Chapter 8 How to upload Artifacts in GitHub
    Chapter 9 How to run Gradle tests with GitHub Actions
    Chapter 10 How to run SpringBoot tests with GitHub Actions
    Chapter 11 How to run Serenity tests with GitHub Actions – NEW
    Chapter 12 How to run Rest API tests with GitHub Actions – NEW
    Chapter 13 How to host Extent Report on GitHub Pages with Github Actions – NEW

    How to create a new repository on GitHub

    HOME

    In this tutorial, I will explain how we can create a new empty repository in GitHub.

    Implementation Steps

    Step 1 – Login to GitHub using your username and password.

    Step 2 – In the upper-right corner of any page, use the drop-down menu, and select “New repository” or left side of the page, there is a green button “New“.

    Step 3 – Type a name for your repository, and an optional description. Here, I have provided below-mentioned information:

    1. Owner – vibssingh
    2. Repository name – Selenium-TestNG
    3. Description – Demo of Selenium with TestNG (optional)

    Step 4 – Choose repository visibility.

    I’m selecting the “Public” option as it is a free account. You can select the Private option for a paid account (mostly organizations). For more information, see About repositories.”

    Step 5 You can create a “README“, which is a document describing your project. For more information, see About READMEs.”

    • You can create a .gitignore file, which is a set of ignore rules. For more information, see “Ignoring files.
    • You can choose to add a software license for your project. For more information, see “Licensing a repository.”

    Click “Create repository” button.

    Below is the screenshot of the new repository in GitHub.

    Congratulations!!. We have just created a new and empty project in GitLab. Now you can clone this project and start working on it.