【发布时间】:2014-05-01 06:58:37
【问题描述】:
在使用 Java 进行 Selenium Webdriver 编码方面需要您的帮助。
我有一个场景,我创建一个课程名称并将其提交到数据库,然后我需要按我创建的名称搜索课程。 简单的工作流程就是; 1. 在文本框中输入课程名称(这里我随机生成一个字符串,所以它不是硬编码的,我需要准确地检索我在这里输入的内容) 2. 存储键入的名称 3. 在搜索框中输入该名称
private void createCurriculum() throws InterruptedException {
selenium.open("http://url.com");
driver.findElement(By.id("Text1")).clear();
driver.findElement(By.id("Text1")).sendKeys("My Curriculum" + genData.generateRandomAlphaNumeric(10)); // Here I'm randomly generating the name, I need to retrieve what I type here in the next method
//String curName = driver.findElement(By.id("Text1")).getAttribute("value");
//I tried this but it didn't work
Thread.sleep(300);
}
private void searchCurriculum(String curName) throws InterruptedException {
selenium.open("http://url.com");
driver.findElement(By.xpath("//div/input")).sendKeys("curName"); // Here I want to retireve what I previously generated. It's not working
// . . .
另外在main方法中,我也声明了变量。
public class TestCaseCreateCurriculum {
private Selenium selenium;
private WebDriver driver;
GenerateData genData;
public String curName;
// . . .
谁能帮我更正这段代码?
修改后完美运行(感谢 Vageesh Bhasin)
driver.findElement(By.id("Text1")).sendKeys(curName = "My Curriculum" + genData.generateRandomAlphaNumeric(10));
和
driver.findElement(By.xpath("//div/input")).sendKeys(curName);
【问题讨论】:
-
为什么不将课程名称存储在变量中? String curName = "我的课程" + genData.generateRandomAlphaNumeric(10);在你的 sendkeys 调用中使用它。
-
究竟是什么错误和@哪一行?
-
如果您使用
curName变量作为字符串,那么为什么要在sendKeys()中再次将其作为字符串传递 -
@Vageesh Bhasin:谢谢你,谢谢 yaar .. 稍加修改,它就完美地工作了!! driver.findElement(By.id("Text1")).sendKeys(curName = "我的课程" + genData.generateRandomAlphaNumeric(10));并在第二种方法中使用 driver.findElement(By.xpath("//div/input")).sendKeys(curName);
标签: java selenium selenium-webdriver