Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you are using <a href="http://selenium-ide.openqa.org/" rel="noreferrer">Selenium IDE</a> to generates code, then you just get a list of every action that selenium will executes. To me, Selenium IDE is a good way to start or do a fast "try and see" test. But, when you think about maintainability and more readable code, you must write your own code.</p> <p>A good way to achieve good selenium code is use the <a href="http://code.google.com/p/webdriver/wiki/PageObjects" rel="noreferrer">Page Object Pattern</a> in a way that the code represents your navigation flow. Here is a good example that I see in <a href="http://dojofloripa.wordpress.com/2008/04/20/como-usar-tdd-e-page-objects-para-construir-interfaces-web/" rel="noreferrer">Coding Dojo Floripa</a> (from Brazil):</p> <pre><code>public class GoogleTest { private Selenium selenium; @Before public void setUp() throws Exception { selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com/webhp?hl=en"); selenium.start(); } @Test public void codingDojoShouldBeInFirstPageOfResults() { GoogleHomePage home = new GoogleHomePage(selenium); GoogleSearchResults searchResults = home.searchFor("coding dojo"); String firstEntry = searchResults.getResult(0); assertEquals("Coding Dojo Wiki: FrontPage", firstEntry); } @After public void tearDown() throws Exception { selenium.stop(); } } public class GoogleHomePage { private final Selenium selenium; public GoogleHomePage(Selenium selenium) { this.selenium = selenium; this.selenium.open("http://www.google.com/webhp?hl=en"); if (!"Google".equals(selenium.getTitle())) { throw new IllegalStateException("Not the Google Home Page"); } } public GoogleSearchResults searchFor(String string) { selenium.type("q", string); selenium.click("btnG"); selenium.waitForPageToLoad("5000"); return new GoogleSearchResults(string, selenium); } } public class GoogleSearchResults { private final Selenium selenium; public GoogleSearchResults(String string, Selenium selenium) { this.selenium = selenium; if (!(string + " - Google Search").equals(selenium.getTitle())) { throw new IllegalStateException( "This is not the Google Results Page"); } } public String getResult(int i) { String nameXPath = "xpath=id('res')/div[1]/div[" + (i + 1) + "]/h2/a"; return selenium.getText(nameXPath); } } </code></pre> <p>Hope that Helps</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload