简体   繁体   中英

How should I write TestNG test cases so that they can be run one after another (sequentially)?

I have project with this structure of TestNG classes: 在此处输入图片说明

I want to create a .xml file (and I'll create batch file to run it from Jenkins in the future) to be able to run each test class sequentially. How should I write each test?

Here's example of my script:

package CRM;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class logowanie {
WebDriver driver = new FirefoxDriver(); 


@Test
public void test() throws InterruptedException {

driver.findElement(By.id("username")).sendKeys("***");
driver.findElement(By.id("password")).sendKeys("***");
driver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
driver.findElement(By.id("_submit")).click();
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);  
  }

@BeforeMethod
public void beforeTest() {

driver.manage().window().maximize();
driver.get("http://crm.serwis-grupowy-vgp.pl/login");
    }

@AfterMethod
public void afterTest() throws InterruptedException {
driver.findElement(By.xpath("//a[@href='/logout']")).click();
driver.quit();

     }}

Here's .xml script:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false" preserve-order="true">
<test name="Test">
<classes>
  <class name="PP_MartynaDealer.podstrona_mcall_MI"/>
  <class name="PP_MartynaDealer.podstrona_HTG_czerw_sierp_MI"/>
  <class name="PP_MartynaDealer.podstrona_rbp_tab_wykresy_MI"/>
  <class name="PP_MartynaDealer.podstrona_cnptool_MI"/>
  <class name="PP_MartynaDealer.podstrona_zakup_MI"/>
  <class name="PP_MartynaDealer.podstrona_materialy_info_MI"/>
  <class name="PP_MartynaDealer.podstrona_raporty_od_0110_MI"/>
  <class name="PP_MartynaDealer.podstrona_HTG_wrze_gru_MI"/>
  <class name="PP_MartynaDealer.podstrona_materialy_z_wydarzen_MI"/>
  <class name="PP_MartynaDealer.podstrona_bonus_MI"/>
  <class name="PP_MartynaDealer.podstrona_bonus_tabele_MI"/>
  <class name="PP_MartynaDealer.podstrona_akcesoria_podstrony_MI"/>
  <class name="PP_MartynaDealer.podstrona_nora_podstrony_MI"/>
  <class name="PP_MartynaDealer.podstrona_moj_kalendarz_MI"/>
  <class name="PP_MartynaDealer.podstrona_akcesoria_MI"/>
  <class name="PP_MartynaDealer.podstrona_nora_MI"/>
  <class name="PP_MartynaDealer.podstrona_autopart_MI"/>
  <class name="PP_MartynaDealer.podstrona_wydarzenia_MI"/>
  <class name="PP_MartynaDealer.podstrona_pisma_okolne_MI"/>
  <class name="PP_MartynaDealer.podstrona_raporty_do_3009_MI"/>
  <class name="PP_MartynaDealer.podstrona_baza_marketingowa_MI"/>
  <class name="PP_MartynaDealer.logowanie_MartynaDealer"/>
  <class name="PP_MartynaDealer.podstrona_uzytkownicy_MI"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

At the moment browser windows are opened one per each test that is executed, all at once and after the last browser window is opened, test starts in first window and they go sequentially. Should I write tests in a different way, so that I can run them sequentially?

you could add preserve-order="true", so in your xml, it will be like :

<test name="Test" preserve-order="true">

another way to do this is to add

@Test(dependsOnMethods = { "testThatWillBeExecutedBeforeThis" })

to each of your test cases.

I believe the reason why you see a lot of browser windows popup is due to this below line

WebDriver driver = new FirefoxDriver();

This line at the class level causes the browsers to be spun off much before the tests are even beginning to execute. You might want to move this line (the instantiation part and not the declaration part) into a setup method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM