【问题标题】:After Generating webdriver code ,how and where to modify code生成 webdriver 代码后,如何以及在何处修改代码
【发布时间】:2014-08-08 15:33:58
【问题描述】:

我是 selenium 测试用例的新手,我的疑问在生成的 Web 驱动程序代码(java ;j-unit)以及如何运行该代码并在该代码中进行一些更改并在项目中发现错误之间徘徊

在此之前,我可能知道我们想要在哪里更改代码,我们已经生成了什么。

例如:

我记录了登录表单页面,使用 selenium IDE 的验证链接按钮生成 Java/Junit 4/webdriver 代码,并将其作为包导入到 eclipse 中......

我在该登录表单中添加了功能或更改,但我故意忘记保留链接按钮

使用 selenium 测试我之前生成的 Java/Junit 4/webdriver 代码,我想将 O/P 打印为“找不到链接按钮”

   package web;
//import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
//import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
//import org.openqa.selenium.support.ui.Select;

public class webdriver {
  private WebDriver driver;
  private String baseUrl;
  //private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://localhost/";
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  }

  @Test
  public void testUntitled() throws Exception {
    driver.get(baseUrl + "/CRM/Default.aspx");
    driver.findElement(By.id("LoginMain_UserName")).clear();
    driver.findElement(By.id("LoginMain_UserName")).sendKeys("Admin");
    driver.findElement(By.id("LoginMain_Password")).clear();
    driver.findElement(By.id("LoginMain_Password")).sendKeys("xxx");
    try {
      assertTrue(isElementPresent(By.id("LoginMain_UserNameLabel")));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    try {
      assertTrue(isElementPresent(By.id("LoginMain_PasswordLabel")));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    try {
      assertTrue(isElementPresent(By.id("LoginMain_UserName")));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    try {
      assertTrue(isElementPresent(By.id("LoginMain_Password")));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    try {
      assertTrue(isElementPresent(By.id("LoginMain_LoginButton")));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    try {
      assertTrue(isElementPresent(By.id("LinkButtonRegister")));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    driver.findElement(By.id("LoginMain_LoginButton")).click();
    try {
      assertEquals("Webdriver Testingest.,", driver.getTitle());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    try {
      assertTrue(isElementPresent(By.id("ctl00_ctl00_ImageButton1")));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    try {
      assertTrue(isElementPresent(By.id("ctl00_ctl00_ImageButton2")));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    try {
      assertTrue(isElementPresent(By.linkText("Home")));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    try {
      assertTrue(isElementPresent(By.linkText("Sales")));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    try {
      assertTrue(isElementPresent(By.linkText("Masters")));
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);        
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

【问题讨论】:

  • 我有个坏消息要告诉你:需要对 Java 和算法有一些基本的了解才能实现这一点......
  • 设置检查链接按钮是否存在的断言消息。发布您的代码,以便我们告诉您在哪里进行更改。
  • @PavelJanicek 是的,没错.. 我知道基本的 java 编码.. 但不知道如何进行更改
  • @Morvader 在命令中我为该链接按钮使用了 VerifyElementpresent
  • 然后我将该代码作为包导入 Eclipse

标签: java eclipse testing junit selenium-webdriver


【解决方案1】:

要得到你想要的,你应该修改:

try {
      assertTrue("Link button not found",isElementPresent(By.id("LinkButtonRegister")));
} catch (Error e) {
      //Add here any log message if necesary
      System.out.println("Link button not found");

      verificationErrors.append(e.toString());
}

希望对您有所帮助!

【讨论】:

  • @user3563106 你必须修改生成的java代码,验证链接按钮是否存在的try/cath
  • 非常感谢@Morvader!!!这是我第一次执行...我想在哪里参考改进我的网络驱动程序的编码请帮助我:)
猜你喜欢
  • 1970-01-01
  • 2016-01-22
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
  • 1970-01-01
相关资源
最近更新 更多