Test Automation Software for Mac

My system is MacOS

Step 1: install web browser(Chrome, Firefox, Chromium)

My browser: Google Chrome 106.0.5249.119

image

How to check your Google Chrome version?

image

step 2: Download Selenium WebDriver framework

Download link

I put the Path in /Users/linda/Documents/testproj

image

Step 3: Download Selenium Browser Driver

https://www.selenium.dev/documentation/webdriver/getting_started/install_drivers/

I put the Path in /Users/linda/Documents/testproj

image

Choose the ChromeDriver version based on your chrome version!!

image

Choose the ChromeDriver version based on your Mac version!!

If your processor is intel, choose chromedriver_mac64.zip If your processor is Apple M1, choose chromedriver_mac_arm64.zip

How to check processor: About this Mac

image

image

Step 4: Install Java (JDK/JRE 1.8)(this part has some issue!!!)

How to Install Java on MacOS? https://www.geeksforgeeks.org/how-to-install-java-on-macos/

  • How to set your java environment:
$ /usr/libexec/java_home -V

Java Path: /usr/local/Cellar/openjdk/18/libexec/openjdk.jdk/Contents/Home

Set Environment variable:

Check you termial is bash or zsh, then choose one.

1.zsh

vi ~/.zshrc

Add: export JAVA_HOME=/usr/local/Cellar/openjdk/18/libexec/openjdk.jdk/Contents/Home

$ source ~/.zshrc

2.bash

$ open -a ~/.bash_profile

Add: export JAVA_HOME=/usr/local/Cellar/openjdk/18/libexec/openjdk.jdk/Contents/Home

$ source ~/.bash_profile
  • Check the variable
$ echo $JAVA_HOME
  • How to check your java version:
$ java -version
$ javac -version

image

Step 5:Create New Java Project using Visual studio Code

Create New Java Project

  • Open Command Pallete in VS Code (Ctrl / Cmd + Shift + P).
  • Select: Java: Create Java Project…
  • Select: No build tools
  • Choose the project path, I choose /Users/linda/Documents/testproj
  • Open project->Click App.java

image

Reference The Libraries

  • Click on + button on the right of the “Referenced Libraries” row present inside the Java Projects column on the sidebar.

image

  • Select all the .jar files you downloaded on the step 2 and insert.

image

If it looks like this, you’re good to go.

Code & Run

create a new TestSelenium class:

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class TestSelenium {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "/Users/linda/Documents/testproj/chromedriver");
        WebDriver driver = new ChromeDriver();
        Actions actions = new Actions(driver);

        driver.get("http://www.google.com");
        driver.manage().window().maximize();
        Thread.sleep(2000);
        driver.findElement(By.name("q")).sendKeys("SFBU");
        actions.sendKeys(Keys.ENTER).build().perform();
        Thread.sleep(2000);
        driver.navigate().to("https://sfbu.edu");
        Thread.sleep(2000);
        String expectedTitle = "SFBU | San Francisco Bay University | SFBU";
        String actualTitle = driver.getTitle();
        System.out.println("Current open web page title is " + actualTitle);
        if (actualTitle.contentEquals(expectedTitle)) {
            System.out.println("Test Passed!");
        } else {
            System.out.println("Test Failed");
        }
        driver.close();
        driver.quit();
    }
}

Go to Run and click on Run Without Debugging.

Test Result

image

Runing Error

“chromedriver” cannot be opened because the developer cannot be verified.

Solution:

Step 1: find the chromedriver path. My path is: /Users/linda/Documents/testproj/chromedriver

Step 2: Now you need to tell Mac OS to trust this binary by lifting the quarantine. Do this by the following terminal command:

xattr -d com.apple.quarantine com.apple.quarantine /Users/linda/Documents/testproj/chromedriver

Reference

https://medium.com/@krsambhav/setting-up-selenium-using-java-on-vs-code-512fcb7d7279

https://funnelgarden.com/setup-selenium-with-java-on-visual-studio-code/