Selenium java maven testng

Selenium Webdriver Tutorial with JAVA and TestNG (2018 Update)

Selenium Webdriver Tutorial by using JAVA and TestNG article is a perfect starting point for Selenium Test Automation with TestNG. In this article, I will explain how to start to Selenium Webdriver with JAVA and TestNG step by step. If you want to start Selenium with JUnit, you can also read this article. Let’s start to Selenium Automation with TestNG and JAVA.

The Tool Belt for Selenium Webdriver Tutorial with JAVA and TestNG

Web Automation Framework: Selenium Webdriver

Development Kit: JAVA JDK

Add-On For CSS and XPath Selection: Ranorex Selocity

Test Framework: TestNG (The other popular test runner framework is JUnit )

Build System: Maven

Selenium Webdriver Tutorial Step by Step Installations

  • Install Firefox and Chrome
  • Install Ranorex Selocity
  • Install RapidEE
  • Install JAVA JDK
  • Install Maven
  • Install IntelliJ, Download Firefox Driver, Download Chrome Driver

You can check this article for setup instructions.

Selenium Webdriver and TestNG

We will setup lastest settings and start to Selenium Webdriver Automation with TestNG with a basic example. After your first project, you will improve your skills by reading our other selenium webdriver tutorials.

Setup Drivers for Selenium 3.X

After Selenium 3.X, we need to declare browser drivers in our test codes. The easiest solution is to download all drivers in a folder and add this folder’s path in environmental path variable and after restart our PC, our tests will see the browser drivers.

– Download Chrome and Firefox Drivers and save them under C:\drivers folder.

selenium testng

– Then, add this folder in system path variable:

selenium testing

– For Mac users, you can learn how to change paths here .

Maven and POM.XML File

Maven is used to defining project structure, dependencies, build, and test management. Using pom.xml you can configure dependencies needed for building testing and running code. Maven automatically downloads the necessary files from the repositories while building the project.

This pom.xml comprises of:

  • Selenium Dependency is needed to use Selenium browser automation framework in our project.
  • TestNG Dependency is needed to use TestNG test runner framework in our project.
  • Maven-surefire-plugin is used to configure and execute tests. Here plugin is used to configure the TestNG.xml for TestNG test and generate test reports.
  • Maven-compiler-plugin is used to help in compiling the code and using the particular JDK version for compilation.
Читайте также:  Xbckj bp cnhjrb php

You can also add several libraries with their maven dependencies.

selenium testing

TestNG XML File

TestNG is a testing framework and we can run our Selenium Tests by using its annotations such as @BeforeMethod, @Test, @AfterMethod, etc.

We can structure and control our test classes with TestNG.xml file. We should add class names, methods, groups, parameters, in this file to run our test with specific configurations.

The basic structure of TestNG.xml file as shown below:

testng tutorial

TestNG Annotations and Their Meaning

@BeforeSuite: The annotated method runs before all tests in this suite.

@BeforeTest: The annotated method runs before test classes.

@BeforeGroups: The annotated method runs before the group’s tests.

@BeforeClass: The annotated method runs before the first test method which belongs to any of the test groups.

@BeforeMethod: The annotated method runs before each test method.

@Test: The annotated method is a test method. It executes the test.

@AfterMethod: The annotated method runs after each test method.

@AfterClass: The annotated method runs after all the test methods in the current test class.

@AfterGroups: The annotated method runs after the last test method which belongs to any of the test groups.

@AfterTest: The annotated method runs after test classes.

@AfterSuite: The annotated method runs after all tests.

Suite > Test > Class > Method

Test Automation Project with Selenium JAVA and TestNG

Open IntelliJ and create a New Project as shown below:

selenium automation

Select Maven and click Next.

testng vs junit

Fill GroupId and ArtifactId as “FirstSeleniumTestNGExample” and click Next.

selenium webdriver

Write your project name as FirstSeleniumTestNGExample and select your project folder and click Finish button.

testng tutorials

Click Enable Auto-Import (This option automatically imports maven dependencies into your project.)

what is testng

The project structure is shown below.

testng selenium example

Now, we need to add Selenium and TestNG libraries in our project by using their dependencies. In order to get those dependencies, we can go to https://mvnrepository.com/artifact/org.testng/testng and https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java

We should add TestNG and Selenium Dependencies in pom.xml as follows. Maven will automatically download the required files into our project.

  4.0.0 FirstSeleniumTestNGExample FirstSeleniumTestNGExample 1.0-SNAPSHOT   org.seleniumhq.selenium selenium-java 3.12.0   org.testng testng 6.14.3   

Then, right-click the project, select “New”, then click “File”.

selenium testng tutorial

Write “testng.xml” as a file name and click “OK”.

selenium webdriver tutorial

In order to create a test file, right click “java” folder under the “test” folder and select “New” and click “Java Class”.

learn selenium

Give a name to your test as “FirstTest”.

selenium example

Test Scenario:
– Open www.swtestacademy.com
– Get it’s title, prints it on the console, and check that it is correct or not.

import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; /***************************************************************************** * Author: Onur Baskirt * Description: This is the first Selenium TestNG test. * It opens swtestacademy homepage and prints and checks its title. *******************************************************************************/ public class FirstTest < //-----------------------------------Global Variables----------------------------------- //Declare a Webdriver variable public WebDriver driver; //Declare a test URL variable public String testURL = "http://www.swtestacademy.com/"; //-----------------------------------Test Setup----------------------------------- @BeforeMethod public void setupTest ()< //Create a new ChromeDriver driver = new ChromeDriver(); //Go to www.swtestacademy.com driver.navigate().to(testURL); >//-----------------------------------Tests----------------------------------- @Test public void firstTest () < //Get page title String title = driver.getTitle(); //Print page's title System.out.println("Page Title: " + title); //Assertion Assert.assertEquals(title, "Software Test Academy", "Title assertion is failed!"); >//-----------------------------------Test TearDown----------------------------------- @AfterMethod public void teardownTest () < //Close browser and end the session driver.quit(); >>

Now, we need to construct our testng.xml file. We can do it by using IntelliJ or manually. If you use IntelliJ, right-click your test file and click “Create ‘FirstTest’… icon as shown below.

Читайте также:  Php shell php скрипты

testng example

After you click “OK” button, you will see below icon on the right top corner of IntelliJ and if you click Run icon, your selenium testng test will start.

testng xml

If you want to use manually configured testng.xml file, you need to open our testng.xml file and modify it as shown below.

After, right-click to testNG.xml file and run it.

Test Result:

For more information about TestNG please visit here .

[fusion_widget_area name=”avada-custom-sidebar-seleniumwidget” title_size=”” title_color=”” background_color=”” padding_top=”” padding_right=”” padding_bottom=”” padding_left=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” >

Thanks for reading Selenium Webdriver Tutorial by using JAVA and TestNG Tutorial article.
Onur Baskirt

Onur Baskirt is a Software Engineering Leader with international experience in world-class companies. Now, he is a Software Engineering Lead at Emirates Airlines in Dubai.

Источник

Create Selenium WebDriver Maven Project

Maven simplifies the creation of a Selenium WebDriver project by handling dependencies and providing a standardized project structure, making it easier to manage and build the project effectively.

Having a hard time using Maven? Then read this express tutorial to create a Selenium Webdriver maven project using TestNG and Eclipse IDE.

In this article, you’ll find some quick visual instructions to set up a Selenium Webdriver maven project in Eclipse. And it won’t take more than five minutes of your precious time.

If you are already using Webdriver & TestNG with Eclipse, then you may skip to the next section. If you don’t, then check if you’ve TestNG plugin installed in your Eclipse. Just go to Window >> Preferences and search for TestNG. It’ll appear in the window pane if your Eclipse IDE has it.

More Read

How to Create TestNG Test Case in Selenium
Use Maven and Eclipse to Run Webdriver Tests

Next, you should ensure that you’ve Maven integration for Eclipse enabled. You can test it by opening the Eclipse marketplace window and searching for the maven keyword. In the search results, look for the text: Maven Integration for Eclipse. If it’s not already added, then click to install the Maven support.

So you are now ready with all the prerequisites needed to create your first Selenium Webdriver maven project. Next, follow the visual aids and the description given in the below sections.

While you are going through this maven tutorial, we recommend that you should start practicing the below steps. This way you can remember them much more easily.

Create a Maven Project in Eclipse IDE

1. Open Eclipse and right-click on Package Explorer. Select New >> Maven project. And click the Next button.

Selenium WebDriver - Create Maven Project

2. Tick the Create a Simple Project (skip archetype selection)> checkbox and click Next.

Skip Archetype Selection

3. Fill in the group id, artifact id, and name. And click the Finish button to create the project.

Читайте также:  Best text styles in css

Configure Maven Project

4. The project structure would look something like the one shown in the below image.

WebDriver Maven Project - Project Structure

5. Also, you must make sure that the Installed JRE setting is correctly pointing to a JDK path on your system. You can change it from the Preferences window.

Select JDK

6. Every maven project uses a POM file to add the required dependencies. So we’ll also be adding some libs required for our Webdriver maven project. Namely, these are the Selenium and TestNG JAR files. Please check the below screenshot.

Note: Make sure you provide the correct version for each dependency you add in the POM.

Update POM File

The above clip was just for informational purposes. We’ll attach the full POM file in the below section.

1. Go to your project and right-click on the src/main/java folder. Then, click to select “New >> TestNG class”.

2. Assign a proper name to the class. And select the required methods. Also, specify the name of the testng.xml> file as shown below.

WebDriver Maven Project - Create a New TestNG Class

3. Next, you should consider moving the TestNG file to the root of the project. See the below snapshot for reference.

WebDriver Maven Project - Move POM File to the root

Sample Code to Copy into Your Project

We provided the Java source code and the POM file. You need to copy/paste them into your Web Driver Maven project files.

Java source code:

package com.techbeamers; import java.util.concurrent.TimeUnit; //-- import org.openqa.selenium.By; //-- import org.openqa.selenium.WebDriver; //-- import org.openqa.selenium.WebElement; //-- import org.openqa.selenium.firefox.FirefoxDriver; //-- import org.testng.Assert; //-- import org.testng.annotations.AfterClass; //-- import org.testng.annotations.BeforeClass; //-- import org.testng.annotations.Test; public class LoadTest01 < private WebDriver driver; @BeforeClass public void beforeClass() < driver = new FirefoxDriver(); >@AfterClass public void afterClass() < driver.quit(); >@Test public void verifySearchButton() < driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://www.google.com"); String search_text = "Google Search"; WebElement search_button = driver.findElement(By.name("btnK")); String text = search_button.getAttribute("value"); Assert.assertEquals(text, search_text, "Text not found!"); >>

POM File:

 4.0.0 com.techbeamers loadtesting 0.0.1-SNAPSHOT Load Testing Selenium Load Testing Example Using TestNG and Maven  2.53.1 6.9.10   org.seleniumhq.selenium selenium-java $ org.testng testng $ test   

Execution:

To run your WebDriver Maven project, right-click on the testng.xml> file. And select Run As >> TestNG suite. That’s how you should be able to run your code using the TestNG.

Run WebDriver Maven Project with POM File

Since we’ve used Maven in our Webdriver project, we can also utilize its POM file to run our tests. But we’ll need to add a few entries for the following plugins in the POM file.

To include these two plugins, you would need to append the following XML snippet to your project’s POM file.

   org.apache.maven.plugins maven-compiler-plugin 1.8 1.8   org.apache.maven.plugins maven-surefire-plugin 2.18.1  testng.xml     

Conclusion

In conclusion, this tutorial has provided a step-by-step guide on creating a Selenium WebDriver Maven project. By following the instructions, you have learned how to set up the necessary dependencies, configure the project structure, and write code to interact with web elements using WebDriver. With this knowledge, you are equipped to start automating web testing and efficiently developing robust Selenium WebDriver projects. Keep exploring and practicing to enhance your skills in web automation testing with Selenium WebDriver.

Keep Learning,

TechBeamers

Источник

Оцените статью