[英]How to run multiple test cases in a multiple paged application with TestNG
This is the testng.xml 这是testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Test Suite" parallel="methods" >
<test name="Tools QA" >
<classes>
<class name="automation.Prescan">
<methods>
<include name= "Startup" />
<include name ="LoginTest" />
<include name="EntryTest" />
</methods>
</class>
</classes>
</test>
</suite>
followed by my firstTest.java file package automation; 其次是我的firstTest.java文件包自动化;
import org.testng.annotations.*;
import org.testng.annotations.BeforeMethod;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
public class Prescan {
String baseURL = "https://abc/login";
public WebDriver driver;
@BeforeMethod
public void Startup() {
System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver.exe");
System.setProperty("webdriver.gecko.driver", "C:\\GeckoDriver\\geckodriver.exe");
// initialize driver
driver = new ChromeDriver();
// driver = new FirefoxDriver();
// driver.get(baseURL);
}
// @Test (description="Prescan login")
@Test(priority = 1)
public void PrescanLogin() throws Exception {
driver.get(baseURL);
driver.findElement(By.id("clientCode")).sendKeys("A");
driver.findElement(By.id("username")).sendKeys("lates");
driver.findElement(By.id("password")).sendKeys("Ma4");
driver.findElement(By.xpath("//button[contains(.,'Login')]")).click();
Assert.assertEquals("Welcome", driver.getTitle());
Thread.sleep(4000);
}
// @Test (description="Pres")
@Test(priority = 2)
public void PreEntry() throws Exception {
// driver.switchTo().frame("frame");
driver.findElement(By.name("account_number")).sendKeys("A7664685W");
driver.findElement(By.name("inv_date_month")).sendKeys("17");
driver.findElement(By.name("inv_date_day")).sendKeys("07");
driver.findElement(By.name("add")).click();
Assert.assertEquals("Client Login", driver.getTitle());
Thread.sleep(4000);
}
@AfterClass
public void exit() {
driver.quit();
}
}
On running this test, I am getting this error 在运行此测试时,出现此错误
PASSED: PrescanLogin
FAILED: PrescanEntry
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"name","selector":"account_number"}
(Session info: chrome=56.0.2924.87)
(Driver info: chromedriver=2.27.440174 )
When i execute above mentioned xml
file, my execution stops. 当我执行上述
xml
文件时,我的执行停止。
Can anyone help me on this issue? 有人可以帮我解决这个问题吗?
judging from your error; 从您的错误判断; your test is failing here:
您的测试在这里失败:
driver.findElement(By.name("account_number")).sendKeys("A7664685W");
This could be for a number of reasons. 这可能是出于多种原因。 It will help if you could update your answet with some html code of the elements that you are trying to locate.
如果您可以使用要查找的元素的一些html代码来更新您的回答,这将有所帮助。 However, from what you have sent already sent;
但是,从您发送的邮件中已经发送了; it looks that these elements are sitting within a frame and you are not switching on it as you have commented the lines.
看起来这些元素位于框架中,并且您未在注释线条时打开它。 Try to add this line below your after removing the // from the frame (do you know the frame name?):
从框架中删除//后,请尝试在您的下方添加以下行(您知道框架名称吗?):
driver.switchTo().frame("frame");
After you finish locating elements within the frame you can go back with this: 在框架中找到元素之后,您可以返回以下内容:
driver.switchTo().defaultContent();
Also, this method of finding elements like this (without prior waiting from them is not advised, as your selenium script trips over..itself trying to find the element way before it has loaded on the page). 另外,这种查找元素的方法(不建议您事先不等待它们,因为您的硒脚本会在试图加载到页面上之前先尝试查找元素的方式)。 So to avoid this use waitForElement:
因此,为了避免这种情况,请使用waitForElement:
WebDriverWait wait = new WebDriverWait(driver, timeout);
Boolean elementPresent = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(XpathLocator)).isDisplayed();
The above will reutrn TRUE if the element is present and FALSE if it isn't. 如果该元素存在,则上面的内容将为TRUE,否则为FALSE。
Best of luck! 祝你好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.