我的AUT有一个jQuery“noty”,点击一个按钮后出现。 (“Noty”是用于创建消息/通知的jQuery插件。)
消息在屏幕上停留几秒钟然后消失。我担心像卡塔隆的'WebUI.verifyElementPresent()'
这样的方法要快。有没有另一种方法来抓住Katalon Studio
或Selenium
?
是的,有办法处理这种情况:
import org.openqa.selenium.support.ui.WebDriverWait as WebDriverWait
WebDriverWait wait = new WebDriverWait(driver, 20)
wait.until(ExpectedConditions.stalenessOf('your noty WebElement that you have to identify before') )
我找到了一种方法来捕捉noty
消息,如here所述。
这是我的代码:
import ru.yandex.qatools.ashot.AShot
import ru.yandex.qatools.ashot.Screenshot
import ru.yandex.qatools.ashot.coordinates.*
import ru.yandex.qatools.ashot.cropper.*
public class ScreenshotHelper {
public void takeWebElementScreenshot(TestObject object) {
WebElement element = WebUiCommonHelper.findWebElement(object, 20)
WebDriver driver = DriverFactory.getWebDriver();
String fileName = new SimpleDateFormat("yyyyMMddHHmmSSS").format(new Date())
Screenshot screenshot = new AShot().takeScreenshot(driver, element)
try {
if (DriverFactory.getExecutedBrowser().getName()=='HEADLESS_DRIVER'){
ImageIO.write(screenshot.getImage(),'PNG', new File("C:/Users/path_to_working_directory/ErrorScreenshots/HeadlessElementScreenshot"+"_"+fileName+".png"))
} else {
ImageIO.write(screenshot.getImage(),'PNG', new File(System.getProperty("user.dir")+"/ErrorScreenshots/ElementScreenshot"+"_"+fileName+".png"))
}
} catch (Exception e) {
e.printStackTrace()
}
}
}
从同一个类的另一个方法调用此方法:
public void catchNotyMessage(){
TestObject noty_warning = new TestObject().addProperty('css', ConditionType.EQUALS, "div.noty_type_warning")
TestObject noty_error = new TestObject().addProperty('css', ConditionType.EQUALS, "div.noty_type_error")
if (WebUI.verifyElementPresent(noty_error, 1, FailureHandling.OPTIONAL)){
this.takeWebElementScreenshot(noty_error)
}
else if (WebUI.verifyElementPresent(noty_warning, 1, FailureHandling.OPTIONAL)){
this.takeWebElementScreenshot(noty_warning)
}
}