Selenium webdriver 无法在Selenium中使用TestNG参数化

Selenium webdriver 无法在Selenium中使用TestNG参数化,selenium-webdriver,testng,Selenium Webdriver,Testng,我是selenium新手,尝试使用测试参数化用户名和密码字段。此脚本仅为我打开URL,不做更多操作。有人能在这里帮助我,让我知道实现这一目标的适当方式吗 import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.test

我是selenium新手,尝试使用测试参数化用户名和密码字段。此脚本仅为我打开URL,不做更多操作。有人能在这里帮助我,让我知道实现这一目标的适当方式吗

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

 public class Testcase1 {

  WebDriver driver=new FirefoxDriver ();
  driver.get("URL");
 // driver.manage().timeouts().pageLoadTimeout(60,TimeUnit.SECONDS);

@Test
@Parameters({"sUsername","sPassword"})
public  void login()

{    

    WebElement objUsername = driver.findElement(By.xpath("//input[@placeholder='Username']"));
    objUsername.sendKeys("sUsername");

    WebElement objPassword = driver.findElement(By.xpath("//input[contains(@placeholder,'Password')]"));
    objPassword.sendKeys("sPassword");

    WebElement objLogin = driver.findElement(By.xpath("//span[contains(text(),'Login')]"));
    objLogin.click();

    if (driver.findElement(By.xpath("//a[.='LOGOUT']")).isDisplayed())
            {
        System.out.print("Test Case is Passed");
            }
    else
        System.out.print("Test Case is Failed");    
            }

}
XML:

import java.util.concurrent.TimeUnit;

从xml中删除导入语句并重试。

从xml中删除导入语句并重试。

您需要进行以下更改:

第一:将
public void login()
更改为
public void login(字符串用户名、字符串密码)

然后:

objUsername.sendKeys(“sUsername”)
to
objUsername.sendKeys(用户名)


objPassword.sendKeys(“sPassword”)
to
objPassword.sendKeys(密码)

您需要进行以下更改:

第一:将
public void login()
更改为
public void login(字符串用户名、字符串密码)

然后:

objUsername.sendKeys(“sUsername”)
to
objUsername.sendKeys(用户名)


objPassword.sendKeys(“sPassword”)
to
objPassword.sendKeys(密码)

感谢您的帮助,现在脚本从xml中获取用户名和密码。但是,它没有导航到下一个屏幕,并且异常失败。现在它工作了,我在脚本中传递字符串而不是变量。感谢帮助,现在脚本从xml中获取用户名和密码。但是,它没有导航到下一个屏幕,并且异常失败。现在它工作了,我在脚本中传递的是字符串而不是变量
import java.util.concurrent.TimeUnit;

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Smoke">
  <test name="Testcase1">
    <parameter name="sUsername" value="testuser"></parameter>
  <parameter name="sPassword" value="123"></parameter>
  <classes>
        <class name="org.param.testcases.Testcase1">
        </class>
    </classes>

    </test> <!-- Test -->
</suite> <!-- Suite -->