Hello guys, today I will post about DOM, what is and where you will use on Automation.
What is the Document Object Model?
The Document Object Model is a platform- and language-neutral interface that will allow programs and scripts to dynamically access and update the content, structure and style of documents. The document can be further processed and the results of that processing can be incorporated back into the presented page.
Why the Document Object Model?
“Dynamic HTML” is a term used by some vendors to describe the combination of HTML, style sheets and scripts that allows documents to be animated.
DOM lookups in a view
Let’s look at some example code. The first thing you can do is to create a container class called LoginContainer
. This class is used to find the DOM elements of the Login View. For the DOM lookup, you can use the @FindBy
annotation, which takes parameters specifying what to find. In Listing 10, we use the How parameter for the lookup and using as the lookup variable. How options include CSS, ID, and XPATH.
Listing 10. LoginContainer with DOM lookups on Ruby, Cucumber and Selenium
@FindBy(how = How.ID, using = "LoginPage") public WebElement loginPageDiv; @FindBy(how = How.CSS, using = "#LoginPage input[name=username]") public WebElement usernameInput; @FindBy(how = How.CSS, using = "#LoginPage input[name=password]") public WebElement passwordInput; @FindBy(how = How.CSS, using = "#LoginPage span[role='button']") public WebElement submitButton;
A more generically class on Java and Selenium to Find a Element
public void FindElement(Parameters_Config configParam, String idName, String findBy) throws IOException, Exception { switch (findBy) { case "linkText": try { configParam.driver.findElement(By.linkText(idName)); } catch (Exception e) { System.out.println(e.getMessage()); } break; case "className": try { configParam.driver.findElement(By.className(idName)); } catch (Exception e) { System.out.println(e.getMessage()); } break; case "id": try { configParam.driver.findElement(By.id(idName)); } catch (Exception e) { System.out.println(e.getMessage()); } break; case "cssSelector": try { configParam.driver.findElement(By.cssSelector(idName)); } catch (Exception e) { System.out.println(e.getMessage()); } break; case "tagName": try { configParam.driver.findElement(By.tagName(idName)); } catch (Exception e) { System.out.println(e.getMessage()); } break; case "xpath": try { configParam.driver.findElement(By.xpath(idName)); } catch (Exception e) { System.out.println(e.getMessage()); } break; default: Exception e = new Exception(); System.out.println(e.getMessage()); break; } }
Thank you 🙂
Font: http://www.w3.org/DOM/