我有如下的testng.xml文件......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false">
<test name="Test">
<classes>
<class name="com.cigital.myssi.automation.tests.SoftwareTest"/>
<class name="com.cigital.myssi.automation.tests.LoginTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
我有相应的测试类如下....
SoftwareTest.java
public class SoftwareTest extends Verification {
private WebDriver driver;
private SoftwarePage softwarepage;
private LoginPage loginpage;
private Log log = LogFactory.getLog(SoftwareTest.class);
@BeforeClass(alwaysRun = true)
public void setup() throws InterruptedException {
driver = DriverFactory.getDriverInstance();
softwarepage = new SoftwarePage(driver);
loginpage = new LoginPage(driver);
}
@Test(description="Create Critical Software")
public void CreateCriticalProject()
{
log.info("Create Critical Software test case Started...............");
assertEquals(true, loginpage.gotoSignPage());
assertEquals(true,loginpage.LogintoPortal("abc@abc.com", "Pass@123", "Welcome"));
assertEquals(true,softwarepage.CreateCriticalSoftware(DataGenerator.RandomNumber("SAMPLE")));
assertEquals(true,loginpage.Logout());
}
@AfterClass(alwaysRun = true)
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
LoginTest.java
public class LoginTest extends Verification {
private WebDriver driver;
private LoginPage loginPage;
@BeforeClass(alwaysRun = true)
public void setup() throws InterruptedException {
driver = DriverFactory.getDriverInstance();
loginPage = new LoginPage(driver);
}
@Test(description = "SigninPage")
public void SigninPage() {
assertEquals(true, loginPage.gotoSignPage());
}
@Test(description="Login to Portal",dependsOnMethods="SigninPage",dataProviderClass=CredentialsProvider.class,dataProvider="testCasesLogin")
public void LoginToPortal(UserBean newbean) throws InterruptedException
{
assertEquals(true,loginPage.LogintoPortal(newbean.getUsername(),newbean.getPassword(),newbean.getTitle()));
}
@AfterClass(alwaysRun = true)
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}
我已在两个班级中启动了webdrive。问题是每当执行第一个测试,即SoftwareTest,然后浏览器退出。但是,我想按顺序执行下一个测试,即LoginTest。任何人都可以帮助我如何实现这一目标?
此致
答案 0 :(得分:3)
尝试以下新课程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using System.Data.Entity;
using System.Threading.Tasks;
using Live_Friend_Message_With_SignalR.Models;
namespace Live_Friend_Message_With_SignalR
{
public class ChatHub : Hub
{
private static List<Tuple<string, ApplicationUser>> _connectedUsers = new List<Tuple<string, ApplicationUser>>();
public override Task OnConnected()
{
if (Context.User.Identity.IsAuthenticated)
{
using (ApplicationDbContext db = new ApplicationDbContext())
{
var user = db.Users.FirstOrDefault(i => i.UserName == Context.User.Identity.Name);
if (!_connectedUsers.ToList().Exists(i => i.Item1 == Context.ConnectionId && i.Item2 == user))
{
_connectedUsers.Add(new Tuple<string, ApplicationUser>(Context.ConnectionId, user));
}
}
}
else
{
if (Context.User.Identity.Name == null)
{
OnDisconnected(true);
}
}
Clients.All.Notification(_connectedUsers.Count);
return base.OnConnected();
}
public override Task OnReconnected()
{
return base.OnReconnected();
}
public override Task OnDisconnected(bool stopCalled)
{
if (_connectedUsers.ToList().Exists(i => i.Item1 == Context.ConnectionId))
{
_connectedUsers.Remove(_connectedUsers.FirstOrDefault(i => i.Item1 == Context.ConnectionId));
}
Clients.All.Notification(_connectedUsers.Count);
return base.OnDisconnected(stopCalled);
}
}
}
}
将此类扩展到所有其他测试类,例如下面的
public class Config extends Verification{
public static WebDriver driver;
public SoftwarePage softwarepage;
public LoginPage loginpage;
@BeforeSuite
public void setUp(){
driver = DriverFactory.getDriverInstance();
}
@AfterSuite
public void tearDown(){
if (driver != null) {
driver.quit();
}
}
这里不需要AfterClass我希望也在BeforeClass中删除这个'driver = DriverFactory.getDriverInstance();'
如果您有任何问题,请告诉我。
由于
答案 1 :(得分:2)
您正在BeforeClass
方法中为这两个类创建一个驱动程序实例,因此一旦执行该类,就会调用AfterClass
方法并关闭您的驱动程序。
而不是使用BeforeClass
&amp; AfterClass
方法可以使用BeforeSuite
方法启动浏览器,使用AfterSuite
方法退出驱动程序。这可以解决你的目的。
您可以在已创建的2个类中的任何一个中创建此BeforeSuite
和AfterSuite
方法。