2017-05-05 19 views
1

我是一名新手,想通过测试参数化用户名和密码字段。这个脚本只为我打开URL,不做任何事情。有人能帮助我,并让我知道实现这一目标的适当方式吗?无法在Selenium中使用TestNG进行参数化设置

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 --> 

回答

0

你需要做以下修改:

第一:改变public void login()public void login(String userName, String password)

然后:

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

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

+0

感谢您的帮助,现在该脚本需要用户名和传递来自xml的单词。但是,它不会导航到下一个屏幕,并会失败并显示异常。 – sgaurav

+0

你会得到什么错误? – kushal

+0

现在它的工作原理是我在脚本中传递字符串而不是变量 – sgaurav

0

删除XML import语句,然后再试一次。

相关问题