java 如何使用 isElementPresent(By by) 方法来验证我的测试脚本是否正常工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16912357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:22:57  来源:igfitidea点击:

How can I use isElementPresent(By by) method to verify my test script is working properly?

javaseleniumselenium-webdriver

提问by sasa_samsudin

How to use this code :

如何使用此代码:

public boolean isElementPresent(By by)

When I export my test script there will be automatically in the test script, but most of the method below is not used, so there will warnings stated that I'm not using that method..and my test script will be failed.

当我导出我的测试脚本时,测试脚本中会自动出现,但是下面的大部分方法都没有使用,所以会有警告说我没有使用该方法......并且我的测试脚本将失败。

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 cheesecake {
  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 = "https://www.google.com.my/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testCheesecake() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("gbqfq")).clear();
    driver.findElement(By.id("gbqfq")).sendKeys("cheesecake");
    driver.findElement(By.cssSelector("a.q.qs")).click();
    driver.findElement(By.cssSelector("span.mn-dwn-arw")).click();
    driver.findElement(By.xpath("//a[contains(text(),'News')]")).click();
  }

  @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;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alert.getText();
    } finally {
      acceptNextAlert = true;
    }
  }
}

Can I know what is the use of all these methods and how to use it? Because, if I deleted them, my test script can run but even though I put a wrong password also can log in to the system.

我可以知道所有这些方法的用途以及如何使用它吗?因为,如果我删除它们,我的测试脚本可以运行,但即使我输入了错误的密码也可以登录系统。

回答by Arran

You are right, this is auto generated by the IDE but you have incorrectly edited it:

你是对的,这是由 IDE 自动生成的,但你错误地编辑了它:

public boolean isElementPresent(By by) {
    try {
      driver.findElements(by);
      return true;
    } catch (org.openqa.selenium.NoSuchElementException e) {
      return false;
    }
}

Then call it with:

然后调用它:

isElementPresent(By.id("J_idt16:J_idt30"));

It is there to make it easier to determine whether an element is visible on the page. Sometimes you want to have that element and do something with it, other times you just want to know if it's present or not. This is there to make it easy to do so.

它可以更轻松地确定元素是否在页面上可见。有时你想拥有那个元素并用它做一些事情,其他时候你只想知道它是否存在。这样做是为了方便。

If you are having problems with your test script, please post the code you are using and the HTML you are running against or try and reproduce it with a different public facing website.

如果您的测试脚本有问题,请发布您正在使用的代码和您正在运行的 HTML,或者尝试使用不同的面向公众的网站重现它。