Domain Specific Language
RSSS

Domain Specific Languages are methods that are specifically written to make the API in a very readable format. This would enable a good rapport between the developers and business people.

Selenium provides a test domain-specific language (Selenese). Selenese is a set of commands that is used for running the tests.

We can also write our own DSL, specific to our requirement and customise according to the framework.

Wrapping the Selenium API/WebDriver API’s.

Selenium 1.0:

[sourcecode language="java" wraplines="false" collapse="false"]
public void selectFrame(Selenium selenium, String elementName){
selenium.highlight(elementName);
selenium.selectWindow(elementName);
selenium.windowFocus();
}
[/sourcecode]

A similar example in the WebDriver can be written, a few examples below:

Selenium 2.0

WebDriver by itself dont have any DSL , but we can always build on our own.

[sourcecode language="java" wraplines="false" collapse="false"]
public void findElementAndClick(webDriver driver, String elementLocator){
driver.findElement(By.name(elemenLocator)).click();
}
[/sourcecode]

Now you can call the method findElementAndClick to do the click operation.

[sourcecode language="java" wraplines="false" collapse="false"]
public void findElementAndType(webDriver driver, String elementLocator,String testData){
driver.findElement(By.name(elemenLocator)).clear();
driver.findElement(By.name(elemenLocator)).sendKeys(testData);
}
[/sourcecode]

Now you can call only the findElementAndType method to perform the operation, this will be more readable and will avoid code duplication.

[sourcecode language="java" wraplines="false" collapse="false"]
public void findElementAndExecuteJavaScript(webDriver driver, JavascriptExecutor js, Id id){
JavascriptExecutor js =(JavascriptExecutor)driver;
js.executeScript("return document.getElementById(‘id’);");
}
[/sourcecode]

Now you can call findElementAndExecuteJavaScript method to perform the JavaScript operation in your tests.

This is just an example and we can always modify according to the requirement and framework.

Hope this helps. Feel free to share and do follow for more posts.

Leave a Reply

Your email address will not be published. Required fields are marked *