gpt4 book ai didi

java - @Test 方法不等待 selenium 代码完成执行

转载 作者:行者123 更新时间:2023-11-28 20:30:10 25 4
gpt4 key购买 nike

package demoActitime;

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.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class LoginActitime {


private String UN;
private String Pass;
private WebElement username;
private WebElement password;
private WebDriver driver = new FirefoxDriver();


private String Url = "http://demo.actitime.com/";
private String Urlvalid = "http://demo.actitime.com/user/submit_tt.do";
private String expected = null;
private String actual = null;

private String xpathUsername = null;
private String xpathPassword = null;
private String xpathLogin = null;


@BeforeMethod
public void findElements()
{



driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.get(Url);

xpathUsername = "//input[@id='username']";
xpathPassword = "//input[@type='password']";
xpathLogin = "//a[@id='loginButton']/div";
}

@AfterMethod
public void doTask()
{

System.out.println(expected);
driver.findElement(By.xpath(xpathUsername)).clear();
driver.findElement(By.xpath(xpathPassword)).clear();
driver.findElement(By.xpath(xpathUsername)).sendKeys(UN);
driver.findElement(By.xpath(xpathPassword)).sendKeys(Pass);
driver.findElement(By.xpath(xpathLogin)).click();

actual = driver.getTitle();

Assert.assertEquals(actual, expected);
// driver.quit();

}

@Test(priority = 0)
public void LoginValidUNInvalidPass()
{
this.UN="admin";
this.Pass="basheer";

System.out.println("LoginValidUNInvalidPass");
expected = "actiTIME - Login";
}
@Test()
public void LoginValidUNValidPass()
{
this.UN="admin";
this.Pass="manager";
System.out.println("LoginValidUNValidPass");
expected = "actiTIME - Enter Time-Track";

}

@Test
public void LoginInValidUNInvalidPass()
{
this.UN="basheer";
this.Pass="basheer";
System.out.println("LoginInValidUNInvalidPass");
expected = "actiTIME - Login";
}

@Test
public void LoginInValidUNValidPass()
{
this.UN="basheer";
this.Pass="manager";
System.out.println("LoginInValidUNValidPass");
expected = "actiTIME - Login";
}





}

这是我的更新代码。我已经删除了初始化,在aftermethod中查找元素并放置在beforemethod中。当我传递有效的用户名和密码时,@After 方法不会等待 Web 驱动程序登录,它显示测试执行已完成。

最佳答案

试试下面的示例程序,它可能会对您有所帮助。我重组了您的代码并使用了@BeforeTest 和@AfterTest。让我知道它是否适合您。

package demoActitime;

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.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class LoginActitime {
//defining all required variables
private String UN = "";
private String Pass = "";
private WebElement username = null;
private WebElement password = null;
private WebElement login = null;
private WebDriver driver = null;

private String Url = "http://demo.actitime.com/";
private String Urlvalid = "http://demo.actitime.com/user/submit_tt.do";
private String expected = null;
private String actual = null;

private String xpathUsername = null;
private String xpathPassword = null;
private String xpathLogin = null;

@BeforeTest
public void findElements()
{
//initialising webdriver with url
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.get(Url);
//initialising webelements
xpathUsername = "//input[@id='username']";
xpathPassword = "//input[@type='password']";
xpathLogin = "//a[@id='loginButton']/div";

username = driver.findElement(By.xpath(xpathUsername));
password = driver.findElement(By.xpath(xpathPassword));
login = driver.findElement(By.xpath(xpathLogin));
}

@AfterTest
public void doTask()
{
System.out.println(expected);
username.clear();
password.clear();
}

@Test(priority = 0)
public void invalidLogin()
{
this.UN="validUser";
this.Pass="invalidPassword";

username.sendKeys(UN);
password.sendKeys(Pass);
login.click();

expected = "expected title";

actual = driver.getTitle();
Assert.assertEquals(actual, expected);
}

@Test(priority = 1)
public void validLogin()
{
this.UN="validUser";
this.Pass="validPassword";

username.sendKeys(UN);
password.sendKeys(Pass);
login.click();

expected = "expected title";

actual = driver.getTitle();
Assert.assertEquals(actual, expected);
}

}

关于java - @Test 方法不等待 selenium 代码完成执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31375911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com