0
点赞
收藏
分享

微信扫一扫

WebDriver的智能等待

寒羽鹿 2022-08-02 阅读 164

package com.gloryroad.Demo;



import org.junit.internal.runners.statements.ExpectException;

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.ExpectedCondition;

import org.openqa.selenium.support.ui.WebDriverWait;

import org.testng.Assert;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Test;



public class textExplictWati {




String url="http://127.0.0.1:8020/HTMLDemo/HTMLPDir/Temp02/Fruit.html";

public WebDriver driver;

@BeforeMethod

public void setUp(){

driver=new FirefoxDriver();

driver.get(url);


}



@AfterMethod

public void tearDown(){

driver.close();

}


@Test

public void testExplictWait(){

try {

WebElement element=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>() {



@Override

public WebElement apply(WebDriver D) {

// TODO Auto-generated method stub

return D.findElement(By.xpath("//*[@type='text']"));

}

});


Assert.assertEquals("今年西瓜特别甜", element.getAttribute("value"));


//2

Boolean contentText=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {



@Override

public Boolean apply(WebDriver driver) {

// TODO Auto-generated method stub

return driver.findElement(By.xpath("//p")).getText().contains("喜欢");

}

});

Assert.assertTrue(contentText);


//3

Boolean inputTextisVisibleFlag=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {



@Override

public Boolean apply(WebDriver driver) {

// TODO Auto-generated method stub

return driver.findElement(By.xpath("//*[@type='text']")).isDisplayed();

}

});

Assert.assertTrue(inputTextisVisibleFlag);

} catch (Exception e) {

// TODO: handle exception

Assert.fail("页面元素没有被找到");

e.printStackTrace();

}

//1



}


public WebElement ElementExplictWait(By locator){


WebElement element=(new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>() {



@Override

public WebElement apply(WebDriver driver) {

// TODO Auto-generated method stub

return driver.findElement(locator);

}

});

return element;



}





}

举报

相关推荐

0 条评论