2

I started to learn about automation testing and I am trying to testing with Intellij using apache-maven 1.8 & selenium-server. I have also installed JDK11. and I try to exercise by testing web, so I created two classes which are homepage & apptest.

Here is my code (homepage.java)

package org.example.page;
import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import static junit.framework.Assert.assertEquals;

public class homepage {
private WebDriver webDriver;

public homepage(WebDriver driver) {
    this.webDriver = driver;
}

public void assertOpenHomePage() throws InterruptedException {
    Thread.sleep(3000);
    String welcomeHomePage = String.valueOf(webDriver.findElement(By.xpath
            ("//strong[contains(text(),'Selenium Easy!')]")).getText());

    assertEquals("Selenium Easy!", welcomeHomePage);


}

and this is the code for the apptest (apptest.java)

package org.example;
import junit.framework.TestCase;
import org.example.page.homepage;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    WebDriver webDriver;

    @Before
    public void initiate(){
        ChromeOptions options = new ChromeOptions();
        options.addArguments("Start-Maximized");
        webDriver = new ChromeDriver(options);
    }

    @Test
    public void openHomePage() throws InterruptedException {
        homepage homepage = new homepage(webDriver);
        webDriver.get("https://www.seleniumeasy.com/");
        homepage.assertOpenHomePage();

        }

}

but when i run the apptest.java, there such an error report:

junit.framework.AssertionFailedError: No tests found in org.example.AppTest
at junit.framework.Assert.fail(Assert.java:57)
at junit.framework.TestCase.fail(TestCase.java:223)
at junit.framework.TestSuite$1.runTest(TestSuite.java:96)

Please help me to resolve this problem, thank you so much!

osundblad
  • 2,675
  • 1
  • 29
  • 34

1 Answers1

0

If using that particular junit library is not mandatory, you may try this junit-jupiter

enter image description here

Page Class:

package pages;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

public class HomePage {
    private WebDriver webDriver;

    public HomePage(WebDriver driver) {
        this.webDriver = driver;
    }

    public String assertOpenHomePage() throws InterruptedException {
        Thread.sleep(3000);
        String welcomeHomePage = String.valueOf(webDriver.findElement(By.xpath
                ("//strong[contains(text(),'Selenium Easy!')]")).getText());
        return welcomeHomePage;
    }
}

TestClass:


import io.github.bonigarcia.wdm.WebDriverManager;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import pages.HomePage;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AppTest {
    static WebDriver webDriver;

    @BeforeAll  
    public static void initiate(){
        WebDriverManager.chromedriver().setup();
        ChromeOptions options = new ChromeOptions();
        options.addArguments("Start-Maximized");
        webDriver = new ChromeDriver(options);
    }

    @Test
    public void openHomePage() throws InterruptedException {
        HomePage homepage = new HomePage(webDriver);
        webDriver.get("https://www.seleniumeasy.com/");
        assertEquals("Selenium Easy!", homepage.assertOpenHomePage());
    }
}

My pom.xml dependencies:

 <dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.0</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.4.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>5.2.3</version>
        </dependency>


    </dependencies>

Or we can also use TestNG that has even more flexibility.

Ahamed Abdul Rahman
  • 512
  • 1
  • 5
  • 18