Many people find it very challenging to convert their normal Selenium Tests scripts to adhere the WebDriver Samplers in JMeter. If you are very much used to the fact of writting Selenium scripts in a day to day basis, writing them on the WebDriver Sampler can not be very tedious. I faced a few challenges in the use of Selenium in JMeter when working on client projects. Following are 5 of those challenges and ways on how I over came them.
- Overcome unwanted crashes due to delay in page load
The test scripts usually tend to fail when the Selenium Driver tries to search for a WebElement way before the page could completely load. For this we usually use the ExplicitWait() function in the normal selenium scripting world. In the WebDriver Sampler you could use the following.
var pkg = JavaImporter(org.openqa.selenium) var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait) var ui = JavaImporter(org.openqa.selenium.support.ui) var wait = new support_ui.WebDriverWait(WDS.browser, 7000) //Here 7000 is the amount of milliseconds that code wait till it sees the specified username element wait.until(ui.ExpectedConditions.visibilityOfElementLocated(pkg.By.ByXPath("//input[@name='username']")))
- Can screenshots be taken for my JMeter Selenium Tests? Yes, with unique names by using parameters.
You would like to take screenshots of your tests and verify them at the end of the test as the JMeter tests run so quickly and you can have it part of your test execution. This can be achieved in the following manner.
var fileUtils = JavaImporter(org.apache.commons.io) var io = JavaImporter(java.io) var location = 'D:/Users/cthalayasingam/Documents/jmeterTest/' var screenshot = WDS.browser.getScreenshotAs(pkg.OutputType.FILE) fileUtils.FileUtils.copyFile(screenshot, new io.File(location+'messages1.png'))
- Can we use the JMeter User Defined Variables or JMeter Functions in the Selenium WebDriver Sampler Script? Yes, Can be achieved in the following ways (the first method is more preferred)
If the JMeter variable is called cDate and you need to use it in send keys or some specific place in the code as shown below.
The other is to mention the variable in the parameters field and then call it as shown in the following manner.
- Checking if an element is present or displayed.
WDS.browser.findElement(pkg.By.ByXPath("//div[contains(@class,'column2')]//div[contains(@class, 'dashlet')]//div[contains(.,'Resend Messages')]")).isDisplayed())
- Logging info messages.
WDS.log.info('Messages all are deleted');
- How can we perform Drag And Drop function?
var actions = new org.openqa.selenium.interactions.Actions(WDS.browser) wait.until(ui.ExpectedConditions.visibilityOfElementLocated(pkg.By.ByXPath("//ul[@class='availableList']//div[contains(@title,'Resend Messages')]"))) var resendDashlet = WDS.browser.findElement(pkg.By.ByXPath("//ul[@class='availableList']//div[contains(@title,'Resend Messages')]")) var col2 = WDS.browser.findElement(pkg.By.ByXPath("//ul[@id='template_x002e_customise-dashlets_x002e_customise-user-dashboard_x0023_default-column-ul-2']")) //the element resendDashlet needs to be dragged and dropped into col2 actions.clickAndHold(resendDashlet).moveToElement(col2).release(col2).build().perform()
There are many out there who had a few queries on how can the above be achieved. Hope the above tips were helpful. If there are more will keep updating this page or create more.