我创建了两个testng类,并在下面的测试套件中包含了这些类名,然后运行了这些案例。但我看到跳过了quitApplication测试。请帮忙。
public class GmailLogIn {
static WebDriver driver;
@Test
public void startBrowser() {
/*
* System.setProperty("webdriver.chrome.driver",
* "E:\\Selenium drivers\\chromedriver.exe"); driver= new
* ChromeDriver();
*/
driver = new FirefoxDriver();
driver.get("https://accounts.google.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(50000, TimeUnit.SECONDS);
String url = driver.getCurrentUrl();
Assert.assertTrue(url.contains("identifier"));
}
@Test(dependsOnMethods = "startBrowser")
public void loadApplication() throws Exception {
driver.findElement(By.id("identifierId")).sendKeys("leninkumaryenni");
driver.findElement(By.xpath(".//*[@class='RveJvd snByac']")).click();
Thread.sleep(3000);
driver.findElement(By.name("password")).sendKeys("Lenin2925");
driver.findElement(By.xpath(".//*[@class='RveJvd snByac']")).click();
String text = driver.findElement(By.className("ZrQ9j")).getText();
Assert.assertEquals("Len", "Len");
}
}
public class Test2 {
@Test()
public void quitApplication(WebDriver driver) {
driver=GmailLogIn.driver;
driver.quit();
}
}
答案 0 :(得分:0)
因为Test2属于类似的类,并且您已经从gmail登录单独执行了作为testng套件的运行。这种情况类似于有两个java类文件,每个文件都有一个main方法,你已经单独从A类执行了main方法,并且没有执行B类main方法。
一个简单的解决方法,将gmaillogin扩展到test2并将test2作为testng套件运行。它将被读作类test2扩展gmaillogin。
希望这会有所帮助。感谢。
答案 1 :(得分:0)
以下是您的问题的答案:
您的代码看起来不错,但您的设置可能有错误。
正如您提到的I have included those class names in below test suite
,因此当您执行套件时,您会看到org.testng.TestNGException
错误为Cannot inject @Test annotated Method [quitApplication] with [interface org.openqa.selenium.WebDriver]
,因为在 Test2 类中您定义了 quitApplication()方法接受WebDriver
实例,即driver
作为public void quitApplication(WebDriver driver)
中的参数。但是你并没有从任何地方传递它。
这里有2个解决方案如下:
instance of the driver
)的序列中执行2个类的想法)你必须将WebDriver
实例即driver
传递给Test2
}类并编写一个构造函数,以便方法quitApplication(WebDriver driver)
被执行。quitApplication()
类中取出Test2
方法并将其放入GmailLogIn
类中。删除参数WebDriver quitDriver
。将注释@AfterTest
添加到quitApplication()
方法。从(dependsOnMethods = "startBrowser")
方法中删除loadApplication()
。而不是@Test
将startBrowser()
置于@BeforeTest
注释中。您的工作代码如下:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
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 GmailLogIn
{
static WebDriver driver;
@BeforeTest
public void startBrowser()
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("https://accounts.google.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(50000, TimeUnit.SECONDS);
String url = driver.getCurrentUrl();
Assert.assertTrue(url.contains("identifier"));
}
@Test
public void loadApplication() throws Exception
{
driver.findElement(By.id("identifierId")).sendKeys("leninkumaryenni");
driver.findElement(By.xpath(".//*[@class='RveJvd snByac']")).click();
Thread.sleep(3000);
driver.findElement(By.name("password")).sendKeys("Lenin2925");
driver.findElement(By.xpath(".//*[@class='RveJvd snByac']")).click();
/*String text = driver.findElement(By.className("ZrQ9j")).getText();
Assert.assertEquals("Len", "Len");*/
}
@AfterTest
public void quitApplication()
{
System.out.println("Quiting Driver");
driver.quit();
System.out.println("Application Closed");
}
}
控制台上的输出将是:
1500868669911 geckodriver::marionette INFO Starting browser C:\Program Files\Mozilla Firefox\firefox.exe with args ["-marionette"]
1500868675056 Marionette INFO Listening on port 60658
Jul 24, 2017 9:27:55 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Quiting Driver
1500868681737 Marionette INFO New connections will no longer be accepted
Application Closed
PASSED: loadApplication
如果这回答你的问题,请告诉我。