【问题标题】:How to store a text field value and retrieve it later - Selenium Web Driver Using Java如何存储文本字段值并稍后检索 - Selenium Web Driver Using Java
【发布时间】: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


【解决方案1】:

你可以这样做:

String course = "My Curriculum" + genData.generateRandomAlphaNumeric(10);
driver.findElement(By.id("Text1")).sendKeys(course); 

稍后您可以在代码中的任何位置使用存储变量。

【讨论】:

    【解决方案2】:

    您可以将值作为属性存储在类中,如果它不能解决您的问题,您可以序列化该值以在需要时恢复。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      • 2018-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多