当我运行XML时,第一个类正在运行。第一类包含@beforeTest
和@AfterTest
注释。我的@Test
注释是在第二类,但它没有运行。
<suite name = "smsweb">
<test name = "login">
<classes>
<class name = "testNG.datapro" /> //Running
</classes>
</test>
<test name = "form">
<classes>
<class name = "testNG.assetscat" /> //not running
</classes>
</test>
</suite>
- 第一类“datapro”
package testNG;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class datapro {
WebDriver driver;
@BeforeTest
public void webopen() throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\saad bin usman\\Downloads\\Compressed\\chromedriver_win32_2\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
Actions a = new Actions(driver);
driver.get("http://test.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
SoftAssert softassert = new SoftAssert();
String title = driver.getTitle();
String exptitle = "Cloud Tenants - Sales Management System";
String user = "admin";
String pass = "admin";
Assert.assertEquals(title, exptitle);
String url = driver.getCurrentUrl();
driver.findElement(By.xpath(".//*[@id='txtUsername']")).clear();
driver.findElement(By.xpath(".//*[@id='txtUsername']")).sendKeys(user);
driver.findElement(By.xpath(".//*[@id='txtPassword']")).clear();
driver.findElement(By.xpath(".//*[@id='txtPassword']")).sendKeys(pass);
Thread.sleep(3000);
driver.findElement(By.xpath(".//*[@id='section-login']/div/div/div[4]/button")).click();
Thread.sleep(4000);
String expectedurl = driver.getCurrentUrl();
Assert.assertNotEquals(url, expectedurl);
String formname = "asset group";
Thread.sleep(6000);
driver.findElement(By.xpath(".//div[@class='sidebar-toggler hidden-phone']")).click();
driver.findElement(By.xpath(".//*[@id='txtMenuSearch']")).clear();
driver.findElement(By.xpath(".//*[@id='txtMenuSearch']")).sendKeys(formname);
Thread.sleep(2000);
WebElement searcharea = driver.findElement(By.xpath(".//ul[@class='page-sidebar-menu']"));
WebElement abc = searcharea.findElement(By.tagName("ul"));
List<WebElement> xyz = abc.findElements(By.tagName("a"));
System.out.println("No of Count Related Your Search " + xyz.size());
int count = 0;
for (int i = 0; i < xyz.size(); i++) {
if (formname.equalsIgnoreCase(xyz.get(i).getText())) {
System.out.println("Form Name Which System Open " + xyz.get(i).getText());
count++;
xyz.get(i).click();
break;
}
}
if (count < 1) {
}
WebElement formpagename = driver
.findElement(By.xpath(".//div[@class='portlet-title']/div[@class='caption span6 text-left']"));
softassert.assertEquals(formpagename.getText().equalsIgnoreCase(formpagename.getText()),
formname.equalsIgnoreCase(formpagename.getText()));
Thread.sleep(5000L);
try {
driver.findElement(By.xpath(".//*[@id='btnAddRecord']/i")).click();
softassert.assertEquals(formpagename.getText().equalsIgnoreCase(formpagename.getText()),
formpagename.getText().equalsIgnoreCase(formpagename.getText()));
} catch (Exception e) {
System.out.println("Add New Record's Button Not Found");
}
softassert.assertAll();
Thread.sleep(8000L);
}
@AfterTest
public void closebrowser() {
driver.quit();
}
- 第二类“assetscat”
package testNG;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class assetscat {
@Test(priority = 1)
public void entername() throws InterruptedException {
datapro d = new datapro();
SoftAssert sa = new SoftAssert();
d.driver.findElement(By.xpath(".//*[@id='btnAddEditSave']")).click();
d.driver.findElement(By.xpath(".//*[@class='icon-remove-circle']")).click();
System.out.println(d.driver
.findElement(By.xpath(".//*[@id='frmAddEdit']//div[@class='control-group required error']")).getText());
// WebElement notisyb
// =d.driver.findElement(By.xpath(".//*[@id='errorNotificationBtn']"));
// System.out.println(notisyb.getText());
List<WebElement> errorelement = d.driver.findElements(By.xpath(
".//*[@id='frmAddEdit']//div[@class='control-group required error']/div/*[@data-dbpropertyname]"));
System.out.println("Mendatory Fields are ");
for (int er = 0; er < errorelement.size(); er++) {
System.out.println(errorelement.get(er).getAttribute("data-dbpropertyname"));
}
for (int e = 0; e < errorelement.size(); e++) {
// System.out.println(errorelement.get(e).getAttribute("data-dbpropertyname"));
if (errorelement.get(e).getAttribute("data-dbpropertyname").equalsIgnoreCase("Name")) {
WebElement savebut = d.driver.findElement(By.xpath(".//*[@id='btnAddEditSave']"));
errorelement.get(e).sendKeys(" ");
savebut.click();
WebElement notipannel = d.driver.findElement(By.xpath(".//*[@class='icon-remove-circle']"));
if (notipannel.isDisplayed()) {
notipannel.click();
}
List<WebElement> cond1 = d.driver.findElements(By.xpath(
".//*[@id='frmAddEdit']//div[@class='control-group required error']/div/*[@data-dbpropertyname]"));
sa.assertEquals(errorelement.size(), cond1.size());
errorelement.get(e).clear();
errorelement.get(e).sendKeys("@#$%^");
savebut.click();
if (notipannel.isDisplayed()) {
notipannel.click();
}
List<WebElement> cond2 = d.driver.findElements(By.xpath(
".//*[@id='frmAddEdit']//div[@class='control-group required error']/div/*[@data-dbpropertyname]"));
sa.assertEquals(errorelement.size(), cond2.size());
errorelement.get(e).clear();
errorelement.get(e).sendKeys("CHaracter20000CHaracter20000CHaracter20000CHaracter20000CHaracter20000");
savebut.click();
if (notipannel.isDisplayed()) {
notipannel.click();
}
List<WebElement> cond3 = d.driver.findElements(By.xpath(
".//*[@id='frmAddEdit']//div[@class='control-group required error']/div/*[@data-dbpropertyname]"));
sa.assertEquals(errorelement.size(), cond3.size());
sa.assertAll();
if (notipannel.isDisplayed()) {
notipannel.click();
}
try {
WebElement datasave = d.driver.findElement(By.xpath(".//*[@class='notification alert-success']"));
if (datasave.isDisplayed()) {
System.out.println("Afetr Enter " + errorelement.get(e).getAttribute("data-dbpropertyname")
+ " Transaction Sucussfully Saved");
break;
}
} catch (Exception e1) {
System.out.println("After Enter " + errorelement.get(e).getAttribute("data-dbpropertyname")
+ " Transaction not Saved");
}
}
else if (errorelement.get(e).getAttribute("data-dbpropertyname").equalsIgnoreCase("ShortName")) {
errorelement.get(e).clear();
errorelement.get(e).sendKeys("Sadi");
d.driver.findElement(By.xpath(".//*[@id='btnAddEditSave']")).click();
WebElement notipannel = d.driver.findElement(By.xpath(".//*[@class='icon-remove-circle']"));
if (notipannel.isDisplayed()) {
notipannel.click();
}
try {
WebElement datasave = d.driver.findElement(By.xpath(".//*[@class='notification alert-success']"));
if (datasave.isDisplayed()) {
System.out.println("Afetr Enter " + errorelement.get(e).getAttribute("data-dbpropertyname")
+ " Transaction Sucussfully Saved");
break;
}
} catch (Exception e1) {
System.out.println("After Enter " + errorelement.get(e).getAttribute("data-dbpropertyname")
+ " Transaction not Saved");
}
}
else if (errorelement.get(e).getAttribute("data-dbpropertyname").equalsIgnoreCase("Colorcode")) {
errorelement.get(e).sendKeys("Black");
d.driver.findElement(By.xpath(".//*[@id='btnAddEditSave']")).click();
WebElement notipannel = d.driver.findElement(By.xpath(".//*[@class='icon-remove-circle']"));
if (notipannel.isDisplayed()) {
notipannel.click();
}
try {
WebElement datasave = d.driver.findElement(By.xpath(".//*[@class='notification alert-success']"));
if (datasave.isDisplayed()) {
System.out.println("Afetr Enter " + errorelement.get(e).getAttribute("data-dbpropertyname")
+ " Transaction Sucussfully Saved");
break;
}
} catch (Exception e1) {
System.out.println("After Enter " + errorelement.get(e).getAttribute("data-dbpropertyname")
+ " Transaction not Saved");
}
}
}
}
}
答案 0 :(得分:0)
试试这个,它应该有效。
<suite name = "smsweb">
<test name = "login">
<classes>
<class name = "testNG.datapro" />
</classes>
<classes>
<class name = "testNG.assetscat" />
</classes>
</test>
</suite>
答案 1 :(得分:0)
TestNG保证@BeforeTest
带注释的方法每<test>
个标签只执行一次。您还没有向我们展示您的测试代码。但是按照你的描述,我猜你的两个测试类都有@Test
个方法,这些方法依赖于一个预期通过@BeforeTest
方法初始化的变量,这个@BeforeTest
方法可能也许一些共同基类的一部分。
如果您的用例绝对需要每<test>
只进行一次初始化,那么您可能需要执行以下操作:
将初始化逻辑移动为
的一部分您可以参考我的this博客文章以获取更多详细信息以及一些显示所有这些内容的示例。
查看来源代码后的更新
你完全弄错了。实例化类和TestNG实例化类之间存在差异。访问webdriver数据成员时assetscat.entername()
失败并NullPointerException
的原因是因为所有初始化都在webopen()
中完成,并使用@BeforeTest
注释。
要解决此问题,您有两种选择:
webopen()
对象之前,在assetscat.entername()
方法中显式调用方法driver
。assetscat extends datapro
。我还建议你花一些时间处理所有这些注释,以便更好地理解如何使用它们。
答案 2 :(得分:0)
尝试此代码,这将使两个测试都运行
<suite name = "smsweb">
<test name = "login">
<classes>
<class name = "testNG.datapro" />
<class name = "testNG.assetscat" />
</classes>
</test>
</suite>