How can test case sequencing be controlled in Selenium TestNG without relying solely on the priority attribute?

If I have a test class with 10 tests, I can use the priority attribute to determine their order. Are there alternative methods for setting the priority of test cases?

Answer №1

Scenario: Test classes include @Test methods with specified priorities imported from org.testng.annotations.Test;

public class MyTests1 {

@Test(priority = 1)
public void test1() {
    System.out.println("Executing test1 in " + getClass().getSimpleName() + " class");
}

@Test(priority = 2)
public void test2() {
    System.out.println("Running test2 in " + getClass().getSimpleName() + " class");
}

}

This example also demonstrates:

@Test public void Test1() {

}

@Test(dependsOnMethods={"Test1"}) public void Test2() {

}

@Test(dependsOnMethods={"Test2"}) public void Test3() {

}

Answer №2

TestNG utilizes Priority to suggest an order of execution, based on the priority assigned to each test. This is different from explicitly setting a specific order.

To establish a strict order for certain tests, Test Dependencies can be used. If TestA has priority=1 and TestB has priority=2, but A depends on B, then TestNG will prioritize running B first, regardless of the assigned priorities, in order to prevent failure of test A.

Combining both methods can result in a quasi "order of execution."

I respectfully disagree with JeffC's statement that tests should always be independent of each other. While this is generally true in unit testing, there are exceptions.

For instance:

@Test (priority=2)
public void validateAddingMilkToShoppingCart(){
  putMilkInCart();
  validateMilkIsInCart();
}

@Test (priority=1, dependsOnMethods = {"validateAddingMilkToShoppingCart"})
public void validateRemovingMilkToShoppingCart(){
  verifyMilkIsInCart();
  removeMilkFromCart();
  validateCartIsEmpty();
}

In this example, "validateRemovingMilkToShoppingCart" may have higher priority due to the current focus on emptying the shopping cart or recent associated bugs. However, it is essential to only run this test if the prerequisite - adding milk to the cart - has been successful. This approach saves time and resources by avoiding unnecessary failed tests. It also results in a cleaner report indicating a Skip if a feature couldn't be tested due to a bug identified in a previous test.

I hope this clarifies your inquiry.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a bug when running Firefox in headless mode with Selenium and Python

I'm attempting to utilize Firefox headless, the Selenium framework, and Python to retrieve a webpage on Amazon EC2 Ubuntu Linux. Below is a snippet of my code: from selenium import webdriver from selenium.webdriver.firefox.options import Options opti ...

Currently in a state of anticipation as I wait for the GMail page to completely load

I am currently attempting to automate the Gmail page for email verification purposes. After entering the username and password, I need to ensure that the page is fully loaded before proceeding with the next action. Here is the approach I have taken: Sele ...

Searching for child and sub-child elements using Selenium in Python

Trying to write a code that can search for a specific text string in all child and sub-child elements of an already defined element. The sample HTML code below showcases this (please excuse any issues with the html and css): <div class="className&q ...

What is the way to obtain a random element in Java code?

I need to select a random element from my Java code. Let's say I have 4 elements in my code and I want to start running the program from the second element, then switch to another element on each subsequent run. How can I achieve this? first ...

Scrape the web using Python to automatically click the "load more" button until it no longer appears, collecting all tables and saving them in a CSV file

I am interested in downloading all tables from a specific website that lists all doctors in Paris. The website link is provided here: . To access all the names, it requires clicking on the "afficher plus de résultats" button multiple times until all resul ...

Selenium web scraping: encountering an issue where the code is not returning a list. It could be related to the dynamic nature of the crawling

from selenium import webdriver from bs4 import BeautifulSoup driver = webdriver.Chrome('./chromedriver') driver.implicitly_wait(5) driver.get('http://car.bitauto.com/') elem = driver.find_elements_by_class_name('yccmp-brand-item ...

Python Selenium displays an error stating that 'geckodriver must be added to your system's path'

Recently, I've been attempting to use Selenium with Python, but unfortunately keep encountering an error. Traceback (most recent call last): File "/home/lario/.local/lib/python3.5/site-packages/selenium/webdriver/common/service.py", line 74, i ...

Are there any tools available to facilitate sending commands to Selenium through an interpreter?

As a Perl programmer, I am currently engaged in web application testing with Selenium. However, I am in search of an interactive interpreter that would allow me to enter Selenium commands directly at a prompt and have them executed by Selenium. My current ...

What is the best way to replicate tags in selenium when an element has multiple class names?

Extracting information from a webpage where a significant amount of text was concealed behind the "see more" tab. Using selenium to click on all such buttons and then extracting data with beautifulsoup. However, some of these buttons have additional space ...

Having trouble getting my Java tests to interact with various div elements. The browser seems to be getting confused. I tried exporting my tests from Selenium IDE to Java with Junit

REPLY USING YOUR OWN QUESTIONS IN THE COMMENTS When it comes to proficiency with Java, I would rate myself as a 3-4 on a scale of 1 to 10. My challenges may stem from a lack of understanding of Java logic, which could be contributing to the issues I am fa ...

Using Selenium to access and read a PDF document that opens in a new tab, without the need for

My team is facing a challenge in our application where clicking on a link opens a new tab with a dynamically generated PDF. The PDF that is generated opens in a new tab and has the URL as "about:blank". I am unable to verify the content of the PDF using ...

Python refuses to acknowledge the assigned value of a variable

I am facing an issue with my Python code where the value of "scraped_pages" is not being recognized correctly. When I run it in the terminal, scraped pages will increment by 1 every loop until the integer reaches a number higher than "page_nums". If I set ...

Setting up various parent xpath options within the class function

Within the init, I have established two parent XPaths like this: def __init__(self, driver, fieldName): self.driver = driver try: try: self.text = self.driver.find_element_by_xpath("//form[@name='quickedi ...

What is the method to retrieve a string from a specific element in Selenium using Python?

I'm facing a challenge in extracting a specific string from the following HTML element: <textarea class="FormTextArea__textarea form-control" aria-label="Photo Caption" aria-describedby="photo-caption__help" id="photo-caption" rows="3">Exterior ...

The Python script runs into a fatal error and displays an error message

A python script has been developed to interact with a web page and place an online order. The script includes a GUI feature that allows users to select the desired day for their order. Below is the Python code: import easygui import sys from selenium impo ...

Verifying the status of a checkbox label in Java using Selenium

I am currently working on an automation project using Selenium and I'm struggling to figure out how to check the status of a checkbox input. Has anyone else encountered this issue before? Here is a sample code snippet in JavaScript with jQuery: $("i ...

Can you please point me to the location where the setproperty method is defined in order to invoke both IE and Chrome browsers in the below statement?

Can someone please explain where the setProperty method, used in the statement below to invoke IE and Chrome browsers, is defined? System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe"); ...

Transforming xpath using selenium in python

Currently, I'm facing a recurring issue where I find myself needing to use the XPath //*[@id='MainContent']/tbody/tr[18]/td[2] about 90% of the time, while requiring //*[@id='MainContent']/tbody/tr[20]/td[2] for the remaining 10%. ...

Having problems initiating a remote session with WebDriver - encountering a TypeError: string indices should be integers

As one of my initial attempts at writing a Python script, I am working on running a test case on localhost hosted on an IIS server in a local environment. To achieve this, I have made the following adjustments: Changed Firefox Settings to 'No proxy& ...

Enter a string into a concealed search bar using Python Selenium

<input type="text" autocomplete=off autocorrect=off class="select-input" Id="myid_bus21" tabindex="0"> The last 2 characters of the IDs change for each link I use, which is why I used: driver.find_element_by_xpath("//*[contains(I'd, 'myi ...