如何在selenium webdriver中使用单个登录测试类运行多个测试类 [英] How to run multiple testclasses wth single login test class in selenium webdriver

查看:134
本文介绍了如何在selenium webdriver中使用单个登录测试类运行多个测试类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有三个课程文件。在头等舱中,只有一个测试用例用于登录Gmail帐户。其余类只有一个测试用例,两者都用于导航收件箱和发送项目。我创建了testng.xml来运行这些多个类。现在问题是当第一堂课正在执行时,我可以登录Gmail帐户。但是当涉及到二等,我无法导航到Inbox,因为我没有在2级和3级执行登录操作。



所以它抛出了我错误空指针异常。我知道我得到这个例外的原因。但我想只执行一次登录操作,但它应该可用于所有类,这意味着当我要执行第2类和第3类时,它不应该抛出错误。它应该继续从1级开始。我怎样才能完成这项任务。



我用Google搜索了很多东西。他们都没有帮助我。如果你有,请建议我。我也试过扩展我的班级。我没有结果。请指导我在哪里挣扎?

解决方案

您需要捕获页面加载事件以了解您是否已登录Gmail,然后运行相应的代码:加载硒页面的活动 [ ^ ]



Google已拥有用于gmail及其他所有服务的大多数现代语言的API库。为什么不用它呢?更简单: Gmail API概述  |  Gmail API  |  Google Developers [ ^ ]

@Member 11898276



可以通过在@BeforeSuite中添加登录步骤来实现,如下所示,您可以创建不同的类并提及所有TestNG XML文件。

@BeforeSuite

@Parameters({浏览器,用户名,密码})



public void setup(String browser,String username,String password)抛出异常{

if(browser.equalsIgnoreCase(firefox)){

driver = new FirefoxDriver();

} else if(browser.equalsIgnoreCase(chrome)){

System.setProperty(webdriver.chrome.driver,C :\\Users\\chromedriver.exe)



公共类TestBase {

公共静态WebDriver驱动程序;

;

driver = new ChromeDriver();

}

driver.get(https:// www。 G oogle.co.in/);



driver.findElement(By.xpath(// * [@ id ='gbw'] / div / div / div [1] / div [1] / a))。click();

driver.findElement(By.id(Email))。sendKeys(username);

driver.findElement(By.id(next))。click();

Thread.sleep(2000);

driver.findElement(By .id(Passwd))。sendKeys(密码);

driver.findElement(By.id(signIn))。click();

系统。 out.println(driver.getTitle());

}





TestNG XML文件如下:

<!DOCTYPE suite SYSTEMhttp://testng.org/testng-1.0.dtd> 
< suite name =Log Suite Exampleverbose =1>
< parameter name =browservalue =Chrome/>
< parameter name =usernamevalue =Test@mailinator.com/>
< parameter name =passwordvalue =Test1234/> s
< listeners>
< listener class-name =pac1.ListenerClass/>
< / listeners>

< test name =TestNG logs samplepreserve-order =true>
< classes>

< class name =pac1.Inbox>< / class>
< class name =pac1.SentItems>< / class>
< class name =pac1.Spam>< / class>



< / classes>
< / test>

< / suite>


Hi I have three class files. In first class, only one test case is used for login Gmail account. rest of the classes having only one test case and both are used for navigating Inbox and sent items. I have created testng.xml to run these multiple classes. Now the problem is when first class is being executed, I could be log in Gmail account. But when it comes to second class, I am unable to navigate to Inbox as I did not perform login operation in class 2 and class 3.

So it thrown me an error Null Pointer Exception. I know the reason why I am getting this exception. But I want to perform login action only one time but it should be available to all classes which means When I am going to execute class 2 and class 3, it should not throw error. It should continue from the class 1. How I can achieve this task.

I googled a lot. None of them helped me. Please suggest me any idea if you have. I have tried to extend my class also. I got no result. Please guide me where am I struggling?

解决方案

You need to catch the page loaded event to know if your are already signed in to Gmail or not and then run the appropriate code: selenium page loaded event[^]

Google has an API Lib in most modern languages for gmail and all their other services. Why not use that instead? Much simpler: Gmail API Overview  |  Gmail API  |  Google Developers[^]


@Member 11898276

It can be achieved by adding Login steps in @BeforeSuite as below and you can create different classes and mention all in TestNG XML file.
@BeforeSuite
@Parameters({"browser", "username", "password"})

public void setup(String browser, String username, String password) throws Exception{
if(browser.equalsIgnoreCase("firefox")){
driver = new FirefoxDriver();
}else if(browser.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe")

public class TestBase {
public static WebDriver driver;
;
driver = new ChromeDriver();
}
driver.get("https://www.google.co.in/");

driver.findElement(By.xpath("//*[@id='gbw']/div/div/div[1]/div[1]/a")).click();
driver.findElement(By.id("Email")).sendKeys(username);
driver.findElement(By.id("next")).click();
Thread.sleep(2000);
driver.findElement(By.id("Passwd")).sendKeys(password);
driver.findElement(By.id("signIn")).click();
System.out.println(driver.getTitle());
}


TestNG XML file as below:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Log Suite Example" verbose="1">
<parameter name="browser" value="Chrome" />
<parameter name="username" value="Test@mailinator.com"/>
	<parameter name="password" value="Test1234"/>s
	<listeners>
		<listener class-name="pac1.ListenerClass" />
	</listeners>

	<test name="TestNG logs sample" preserve-order="true">
		<classes>
			
	       <class name="pac1.Inbox"></class>
	       <class name="pac1.SentItems"></class>
		<class name="pac1.Spam"></class>
			
				
			
		</classes>
	</test>

</suite>


这篇关于如何在selenium webdriver中使用单个登录测试类运行多个测试类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆