0
0
Selenium Javatesting~20 mins

Why TestNG structures test execution in Selenium Java - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TestNG Execution Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of TestNG Test Execution Structure

Why does TestNG organize tests using annotations like @BeforeMethod, @Test, and @AfterMethod?

ATo make tests run only once without any setup or cleanup
BTo force tests to run in alphabetical order by method name
CTo control the order of test execution and setup/cleanup steps automatically
DTo disable parallel execution of tests by default
Attempts:
2 left
💡 Hint

Think about how setup and cleanup code can be reused for each test.

Predict Output
intermediate
2:00remaining
TestNG Execution Order Output

What will be the output order when running this TestNG test class?

Selenium Java
import org.testng.annotations.*;

public class SampleTest {

  @BeforeMethod
  public void setup() {
    System.out.println("Setup");
  }

  @Test
  public void testA() {
    System.out.println("Test A");
  }

  @Test
  public void testB() {
    System.out.println("Test B");
  }

  @AfterMethod
  public void cleanup() {
    System.out.println("Cleanup");
  }
}
A
Setup
Cleanup
Setup
Cleanup
Test A
Test B
B
Test A
Test B
Setup
Cleanup
Setup
Cleanup
C
Setup
Test A
Test B
Cleanup
Cleanup
D
Setup
Test A
Cleanup
Setup
Test B
Cleanup
Attempts:
2 left
💡 Hint

Remember that @BeforeMethod and @AfterMethod run before and after each @Test method.

assertion
advanced
2:00remaining
Correct Assertion for TestNG Test Result

Which assertion correctly verifies that a web page title equals "Home Page" in a TestNG Selenium test?

Selenium Java
String actualTitle = driver.getTitle();
AAssert.assertEquals(actualTitle, "Home Page");
BAssert.assertTrue(actualTitle = "Home Page");
CAssert.assertEquals("Home Page", actualTitle, false);
DAssert.assertTrue(actualTitle.equalsIgnoreCase("Home Page"));
Attempts:
2 left
💡 Hint

Check the correct method signature for equality assertion in TestNG.

🔧 Debug
advanced
2:00remaining
Identify the TestNG Execution Problem

Given this TestNG test class, why does the @AfterMethod not run after testLogin?

Selenium Java
import org.testng.annotations.*;

public class LoginTest {

  @BeforeMethod
  public void setup() {
    System.out.println("Setup");
  }

  @Test
  public void testLogin() {
    System.out.println("Login Test");
    throw new RuntimeException("Fail");
  }

  @AfterMethod
  public void cleanup() {
    System.out.println("Cleanup");
  }
}
ABecause the exception in testLogin stops the @AfterMethod from running
BBecause @AfterMethod is missing the alwaysRun=true attribute
CBecause @BeforeMethod and @AfterMethod must be static methods
DBecause @AfterMethod only runs if @Test passes without exceptions
Attempts:
2 left
💡 Hint

Consider how TestNG handles cleanup when tests throw exceptions.

framework
expert
3:00remaining
TestNG Parallel Execution Behavior

When TestNG is configured to run tests in parallel by methods, what is the expected behavior regarding @BeforeMethod and @AfterMethod?

A<code>@BeforeMethod</code> and <code>@AfterMethod</code> run in the same thread as their test method, independently for each test
B<code>@BeforeMethod</code> and <code>@AfterMethod</code> run sequentially in the main thread regardless of parallel tests
C<code>@BeforeMethod</code> runs once before all tests, and <code>@AfterMethod</code> runs once after all tests
D<code>@BeforeMethod</code> and <code>@AfterMethod</code> are skipped when tests run in parallel
Attempts:
2 left
💡 Hint

Think about thread safety and how setup/cleanup should behave when tests run at the same time.