我想在Jmeter.Webdriver Webdriver Sampler中进行异常处理
请让我,如何在Jmeter.Webdriver webdriver Sampler中使用try / catch块?
答案 0 :(得分:0)
使用 # check if user is logged in
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if(!$customerSession->isLoggedIn())
{
$request = $objectManager->get('Magento\Framework\App\Request\Http');
//get instance for URL interface
/** @var \Magento\Framework\UrlInterface $urlInterface */
$urlInterface = $objectManager->get('Magento\Framework\UrlInterface');
// URL to redirect to
$url = $urlInterface->getUrl('customer/account/login');
if(strpos($request->getPathInfo(), '/customer/account/') !== 0)
{
# redirect to /customer/account/login
header('Location:'.$url );die();
}
}
围绕代码并在最后添加try block
,方法是catch block
捕获异常。 (在示例中,它是variable name
)
尝试如下:
exc
<{1>}中的,您可以看到try{
WDS.sampleResult.sampleStart()
WDS.browser.get('http://jmeter-plugins.org')
var pkg = JavaImporter(org.openqa.selenium)
WDS.browser.findElement(pkg.By.id('what')) // there is no such element with id what
WDS.sampleResult.sampleEnd()
}
catch(exc){ //exc variable name
WDS.log.error("element not found" + exc)
}
的完整跟踪,这是在尝试按ID查找元素时引发的,其值为JMeter log
,这在NoSuchElementException
中不存在HTML。
注意:使用what
查看View Results in Table
。
参考:
答案 1 :(得分:0)
您可以通过普通的JavaScript try block执行此操作,以下是发生错误时截屏的示例:
var pkg = JavaImporter(org.openqa.selenium)
var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait)
var conditions = org.openqa.selenium.support.ui.ExpectedConditions
var wait = new support_ui.WebDriverWait(WDS.browser, 5)
var exception = null
WDS.sampleResult.sampleStart()
try {
WDS.browser.get('http://example.com')
wait.until(conditions.presenceOfElementLocated(pkg.By.linkText('Not existing link')))
} catch (err) {
WDS.log.error(err.message)
var screenshot = WDS.browser.getScreenshotAs(pkg.OutputType.FILE)
screenshot.renameTo(java.io.File('screenshot.png'))
exception = err
} finally {
throw (exception)
}
WDS.sampleResult.sampleEnd())
不要忘记&#34;扔掉&#34;处理后的错误,否则会被吞下#34;而你得到一个假阳性的结果。
有关更多提示和技巧,请参阅The WebDriver Sampler: Your Top 10 Questions Answered文章
答案 2 :(得分:0)
这和你在eclipse等其他IDE中的表现方式相同。 你可以看到下面的代码
//try block starts here
try{
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element"))).click();
}
catch(Exception e)
{
WDS.log.info("Exception is : " +e);//you can print the exception in jmeter log.
}
如果你使用的是javascript,那么应该用单引号替换双引号因为BeanShell很简单并且它类似于java尽可能多地使用BeanShell