我的 Java
package modules;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import Config.Config;
public class Invalid_Login {
String InvalidLogin_Status = null;
String InvalidLogin_Message = null;
@Test(dataProvider = "emplogin")
@Parameters({"USERNAME", "PASSWORD"})
public void Execute(String UserName, String Password) {
Config.driver.findElement(By.linkText("Log in")).click();
Config.driver.findElement(By.id("user_login")).sendKeys(UserName);
Config.driver.findElement(By.id("user_pass")).sendKeys(Password);
Config.driver.findElement(By.id("wp-submit")).click();
WebElement a = Config.driver.findElement(By.id("login_error"));
System.out.println(a.getText());
Assert.assertEquals("ERROR: Invalid username. Lost your password?", a.getText());
}
@DataProvider(name = "emplogin")
public Object[][] logindata() {
Object[][] credentials = new Object[5][2];
//incorrect login/short login
credentials[1][0] = "testss";
credentials[1][1] = "testss";
//empty login
credentials[2][0] = "";
credentials[2][1] = "empty login";
// empty password
credentials[3][0] = "emptypass";
credentials[3][1] = "";
// all empty
credentials[4][0] = "";
credentials[4][1] = "";
//incorrect
credentials[5][0] = "wrongusername";
credentials[5][1] = "wrongpass";
return credentials;
}
}
我的 XML
<?xml version="1.0" encoding="UTF-8"?>
<suite name="DUMMY" parallel="tests" thread-count="1">
<parameter name="RUNUSINGSG" value="N"/>
<test name="Verify InvalidLogin Functionality By Passing Valid Username and password"/>
<parameter name="BROWSERCODE" value="FF"/>
<parameter name="URL" value="http://demo.rtcamp.com/rtmedia/"/>
<parameter name="VERSION" value="ANY"/>
<parameter name="PLATFORM" value="WINDOWS"/>
<parameter name="TITLE" value="rtcamp - Empowering The Web With Wordpress!"/>
<parameter name="USERNAME" value="swash"/>
<parameter name="PASSWORD" value="123"/>
<classes>
<class name="modules.LaunchApplication"/>
<class name="modules.Login"/>
</classes>
</test>
</suite>
我想测试登录表单的无效数据,我在这里使用数据提供者,我将在 xml 文件中传递数据,所以我想知道我应该在 xml 文件中使用哪些值。例如。我应该在值中使用有效数据还是无效数据?有什么帮助吗?
最佳答案
您不能在 @Test
方法上耦合 @Parameters
和 @DataProvider
注释。
参数和 DataProvider 注释被引入到 TestNG 中,其唯一目的是参数化测试。 它们作为两个选项提供,用于参数化您的测试,但并不意味着要相互结合使用。
出于什么目的,您将此参数 username='swash' 和 password='123' 分别传递给您的测试方法。我假设这些是正确的凭据,否则您可以添加这是您的 testng 数据提供者本身
我建议你有两个数据提供者,一个提供无效的登录凭据,如上面的数据提供者(emplogin),另一个提供有效的登录凭据,如下面的广告,将其传递给检查成功登录的方法
注意:在您的代码中,您使用的是 testng 框架,但您导入了错误的断言包 import org.junit.Assert
;使用 import org.testng.Assert
;相反
编辑:
示例:对于无效的登录类
package modules;
import org.testng.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import Config.Config;
public class Invalid_Login {
String InvalidLogin_Status = null;
String InvalidLogin_Message = null;
@Test(dataProvider = "emplogin")
public void Execute(String UserName, String Password) {
Config.driver.findElement(By.linkText("Log in")).click();
Config.driver.findElement(By.id("user_login")).sendKeys(UserName);
Config.driver.findElement(By.id("user_pass")).sendKeys(Password);
Config.driver.findElement(By.id("wp-submit")).click();
WebElement a = Config.driver.findElement(By.id("login_error"));
System.out.println(a.getText());
Assert.assertEquals(a.getText(),"ERROR: Invalid username. Lost your password?");
}
@DataProvider(name = "emplogin")
public Object[][] logindata() {
Object[][] credentials = new Object[5][2];
//incorrect login/short login
//Array index should start with 0 as we have mentioned[5][2]
//it should contain 5 rows and 2 columns
credentials[0][0] = "testss";//credentials[1][0] = "testss";
credentials[0][1] = "testss";//credentials[1][1] = "testss";
//empty login
credentials[1][0] = "";
credentials[1][1] = "
// empty password
credentials[2][0] = "emptypass";
credentials[2][1] = "";
// all empty
credentials[3][0] = "";//credentials[2][0] = "";
credentials[3][1] = "";
//incorrect
credentials[4][0] = "wrongusername";
credentials[4][1] = "wrongpass";
return credentials;
}
}
Testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="DUMMY" parallel="tests" thread-count="1">
<test name="Verify InvalidLogin Functionality By Passing Valid Username and password">
<classes>
<class name="modules.Invalid_Login"/>
</classes>
</test>
</suite>
您无需在 xml 文件中指定无效的登录参数,数据提供者会将无效的凭据作为对象数组提供给测试方法(执行)。例如尝试上面的代码
希望这对您有所帮助...如果我错误地理解了您的问题或者您有任何疑问。请回来
关于java - 对于无效登录,应该在 xml 中传递哪些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31371418/