有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用selenium检查登录表单中的成功和失败消息?

以下代码用于g-mail登录

package gmail_Login;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Login {

//public static void main(String[] args) {
  // TODO Auto-generated method stub

  // created reference variable for WebDriver
     WebDriver drv;
     @Before
     public void setup() throws InterruptedException {
            // initializing drv variable using FirefoxDriver
            drv=new FirefoxDriver();
            // launching gmail.com on the browser
            drv.get("https://gmail.com");
            // maximized the browser window
            drv.manage().window().maximize();
            drv.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
     }
     @Test
     public void test() throws InterruptedException {
            // saving the GUI element reference into a "username" variable of WebElement type
            WebElement username = drv.findElement(By.id("Email"));
            // entering username
            username.sendKeys("fake mail id");
           // clicking next button
            drv.findElement(By.id("next")).click();

}

我必须给假身份证,然后点击下一步按钮错误消息即将到来,但如何检查错误消息


共 (1) 个答案

  1. # 1 楼答案

    单击“下一步”按钮后,使用以下步骤获取错误消息的文本:

    1. 查找错误消息元素

      WebElement msg=驱动程序。findElement(By.id(“errormsg_0_Email”)

    2. 获取消息文本

      String text=msg。getText()

    3. 使用预期文本断言/验证文本

      断言。assertEquals(文本、预期文本)