Java tutorial
/******************************************************************************* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. *******************************************************************************/ package atd.selenium; import static org.junit.Assert.fail; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.NoAlertPresentException; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class RegisterUser { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { driver = new FirefoxDriver(); baseUrl = "https://atd.plebian.nl/"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Test public void testCsv() throws Exception { String csvFile = "test/atd/selenium/RegisterUser.csv"; BufferedReader br = null; String line = ""; String cvsSplitBy = ","; try { br = new BufferedReader(new FileReader(csvFile)); while ((line = br.readLine()) != null) { // use comma as separator String[] entry = line.split(cvsSplitBy); driver.get(baseUrl + "/ATD-WEBSITE/register/register.jsp"); driver.findElement(By.name("username")).clear(); driver.findElement(By.name("username")).sendKeys(entry[0]); driver.findElement(By.name("password")).clear(); driver.findElement(By.name("password")).sendKeys(entry[2]); driver.findElement(By.name("realname")).clear(); driver.findElement(By.name("realname")).sendKeys(entry[0] + " " + entry[1]); driver.findElement(By.name("postcode")).clear(); driver.findElement(By.name("postcode")).sendKeys(entry[3]); driver.findElement(By.name("email")).clear(); driver.findElement(By.name("email")).sendKeys(entry[4]); driver.findElement(By.name("kenteken")).clear(); driver.findElement(By.name("kenteken")).sendKeys(entry[5]); driver.findElement(By.name("merk")).clear(); driver.findElement(By.name("merk")).sendKeys(entry[6]); driver.findElement(By.name("type")).clear(); driver.findElement(By.name("type")).sendKeys(entry[7]); driver.findElement(By.xpath("//input[@value='Registreren']")).click(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } } @After public void tearDown() throws Exception { driver.quit(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } private boolean isElementPresent(By by) { try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } private boolean isAlertPresent() { try { driver.switchTo().alert(); return true; } catch (NoAlertPresentException e) { return false; } } private String closeAlertAndGetItsText() { try { Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); if (acceptNextAlert) { alert.accept(); } else { alert.dismiss(); } return alertText; } finally { acceptNextAlert = true; } } }