我已经用selenium框架编写了黄瓜测试。当我的测试按顺序运行时,它们都通过了,但是当我并行运行时,它们失败了,因为所有测试都使用同一个用户,并且测试是登录应用程序,更改语言并验证语言,注销并重新登录验证该用户的语言是否持久。 问题是每个测试都是为了验证不同的语言。因此,测试 1 运行并在注销期间重新登录已运行的第二个测试,并且它更改了语言。由于语言不同,测试 1 失败。为此,我在网上读到,使用挂钩可能有助于在并行测试中按顺序运行测试场景。我确实实现了钩子,但它们仍然不起作用。不确定我这样做是否正确。
这是我的功能文件
@Profile @VerifyLanguageSetPortugues
Scenario Outline: Profile tests(Verify App Languages selection Espanol)
Given "guest" user login to app "DARTTS" using "AppAuth"
Then I verify previous language setting is active from app title "<Current AppTitle>"
And I click on language "<Language>"
Then I verify selected language for "<Field1>" "<Field2>" "<Field3>"
And I logout and log back in
And I verify previous language setting is active from app title "<Previous AppTitle>"
When I click on user profile button
Then I should log out of Raven app
Examples:
| Language | Current AppTitle | Previous AppTitle | Field1 | Field2 | Field3 |
| Português | Analyze | Analisar | Clique no botão \"novo grupo de filtros\" para criar seu primeiro grupo de filtros. | Selecione um filtro para mostrar os resultados nesta tela. | Expandir explorar e tabular as estatísticas |
@Profile @VerifyLanguageSetEspanol
Scenario Outline: Profile tests(Verify App Languages selection Espanol)
Given "guest" user login to app "DARTTS" using "AppAuth"
Then I verify previous language setting is active from app title "<Current AppTitle>"
And I click on language "<Language>"
Then I verify selected language for "<Field1>" "<Field2>" "<Field3>"
And I logout and log back in
And I verify previous language setting is active from app title "<Previous AppTitle>"
When I click on user profile button
Then I should log out of Raven app
Examples:
| Language | Current AppTitle | Previous AppTitle | Field1 | Field2 | Field3 |
| Español | Analyze | Analizar | Haga clic en el botón \"nuevo grupo de filtros\" para crear su primer grupo de filtros. | Seleccione un grupo de filtros para mostrar los resultados en esta pantalla. | Ampliar la exploración y las estadísticas de las columnas |
挂钩类
public class Hooks {
@Before(order = 1, value = "@VerifyLanguageSetPortugues")
public void portuguesLangBeforeScenario() {
System.out.println("This should run first");
}
@After(order = 1, value = "@VerifyLanguageSetPortugues")
public void portuguesLangAfterScenario() {
System.out.println("This should end first");
}
@Before(order = 2, value = "@VerifyLanguageSetEspanol")
public void espanolLangBeforeScenario() {
System.out.println("This should run second");
}
@After(order = 2, value = "@VerifyLanguageSetEspanol")
public void espanolLangAfterScenario() {
System.out.println("This should end second");
System.out.println("Espanol language is set to true");
}
在输出中,我看到消息打印在每条消息旁边。第二个场景没有等到第一个场景完成。
11:01:59.545 [TestNG-PoolService-1] INFO gov.dhs.ice.raven.resources.base - Detected Dartts scenario tag.
11:02:05.474 [TestNG-PoolService-0] INFO gov.dhs.ice.raven.resources.base - WebDriver setup is complete
This should run first
11:02:05.548 [TestNG-PoolService-1] INFO gov.dhs.ice.raven.resources.base - WebDriver setup is complete
This should run second
当我的测试按顺序运行时,它们都通过了,但是当我并行运行时,它们失败了,因为所有测试都使用同一个用户,并且测试是登录应用程序,更改语言并验证语言,注销并重新登录来验证该用户的语言是否持久。问题是每个测试都是为了验证不同的语言。因此,测试 1 运行并在注销期间重新登录已运行的第二个测试,并且它更改了语言。由于语言不同,测试 1 失败。
这是竞争条件的典型示例。您有多个线程同时修改同一资源。
因此您需要确保测试不会同时使用同一用户。有很多方法可以实现这一点: