Run First Automation Script with JUnit

Now go to src>test>java

Right click on java folder then go to new and click on Java Class.

Give a name of Java class

Now download geckodriver for firefox and chromedriver for chrome browser.

Extract the file and keep them into src/test/resources location

Now go to IntelIJ editor and add following code:

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class FirstTestCase {
    WebDriver driver;
    @Before
    public void setup(){
        System.setProperty("webdriver.chrome.driver", "./src/test/resources/chromedriver.exe");
        driver=new ChromeDriver();
        driver.manage().window().maximize();
    }
    @Test
    public void Test1() {

        driver.get("https://www.roadtosdet.com/selenium//");
        String text=driver.findElement(By.cssSelector("h1")).getText();
        Assert.assertEquals(text.contains("Selenium"),true);

    }
    @After
    public void finishTest(){
        driver.close();
    }
}

I have configured this for chrome browser. If you want, you can run it through Firefox browser.

Follow this code to run with Firefox:

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FirstTestCase {
    WebDriver driver;
    @Before
    public void setup(){
        System.setProperty("webdriver.gecko.driver", "./src/test/resources/geckodriver.exe");
        driver=new FirefoxDriver();
        driver.manage().window().maximize();
    }
    @Test
    public void Test1() {

        driver.get("https://www.roadtosdet.com/selenium//");
        String text=driver.findElement(By.className("site-title")).getText();
        Assert.assertEquals(text.contains("Road to SDET"),true);

    }
    @After
    public void finishTest(){
        driver.close();
    }
}

Then click on Add Configuration button, then click on “+” icon and select JUnit

Now set the configuration like following screenshot:

Apply and click ok

Now click on run button and you will see automation will start and test gets success.

Here is the full project:
https://github.com/salmansrabon/selenium-junit-demo

about author

admin

salmansrabon@gmail.com

If you like my post or If you have any queries, do not hesitate to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *