无法在Selenium中使用TestNG进行参数化

时间:2017-05-05 16:38:04

标签: 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.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 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 -->

2 个答案:

答案 0 :(得分:0)

从xml中删除import语句,然后重试。

答案 1 :(得分:0)

您需要进行以下更改:

首先:将public void login()更改为public void login(String userName, String password)

然后:

objUsername.sendKeys("sUsername");objUsername.sendKeys(userName);

objPassword.sendKeys("sPassword");objPassword.sendKeys(password);