Hello,
Can we get the data from CSV file in a simple page object project ( not maven project ). I have tried doing that but i am getting NullpointerException. Maybe because it is not able to track the path of the regdata.csv file. I have kept it in the root folder with name resource (just like we make a "lib" folder). Any Ideas ?? Thanks Again!!! Regards |
Administrator
|
Could you post your code snippet?
~ seleniumtests.com
|
Exceltesting.zip
Hi, i have attached the snippet , Please have a look and let me know where i am going wrong . Thanks Regards!! |
Administrator
|
could you post an abridged version of snippet, which makes it easy to debug. Instead of a zip file?
~ seleniumtests.com
|
Hi Tarun, Thanks for your reply .Following is the code. Please have a look
<?xml version="1.0" encoding="UTF-8"?> <suite name="Selenium Tests" verbose="3" parallel="tests"> <test name="Test in Chrome"> <parameter name="browser" value="Chrome"></parameter> <classes> <class name="com.test.DerivedClass" /> </classes> </test> </suite> ------------------------------------------------------------------- public class BaseClass { public WebDriver driver; public String appURL; @BeforeMethod() @Parameters("browser") public void launchBrowser(String browser){ appURL= "http://newtours.demoaut.com/mercuryregister.php"; Reporter.log("Selecting the chrome browser condition"); if (browser.equalsIgnoreCase("Chrome")) { System.setProperty("webdriver.chrome.driver","D://driver/chromedriver.exe"); driver = new ChromeDriver(); } } @AfterMethod public void closeBrowser(ITestResult result) throws IOException { if (!result.isSuccess()) { File imageFile = ((TakesScreenshot) new Augmenter().augment(driver)) .getScreenshotAs(OutputType.FILE); String failureImageFileName = result.getMethod().getMethodName()+ new SimpleDateFormat("MM-dd-yyyy_HH-ss").format(new GregorianCalendar().getTime()) + ".png"; File failureImageFile = new File(failureImageFileName); FileUtils.moveFile(imageFile, failureImageFile); } driver.close(); driver.quit(); } } --------------------------------------------------------------------------------- public class DerivedClass extends BaseClass { @Test(dataProvider = "regCSVData", dataProviderClass = Beanclass.class) public void CSVmethod(Beanclass bean) { driver.get(appURL); Formpage page = new Formpage(driver); page.registeruser(bean); assert driver .findElement(By.tagName("body")) .getText() .contains( "Thank you for registering. " + "You may now sign-in using the user name and password you've just entered."); } } ---------------------------------------------------------------------------------- public class Beanclass { private String firstName; private String lastName; private String phoneNumber; private String userName; private String country; private String email; private String password; private String confirmPassword; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getConfirmPassword() { return confirmPassword; } public void setConfirmPassword(String confirmPassword) { this.confirmPassword = confirmPassword; } @DataProvider(name = "regCSVData") public static Object[][] getCSVData() throws IOException { Reporter.log("inside the getCSVData method"); CSVReader csvReader = new CSVReader(new FileReader(Beanclass.class .getResource( System.getProperty("user.dir") + "//Exceltesting//resource//regdata.csv") .getPath())); List<String[]> dataList = csvReader.readAll(); Object[][] data = new Object[dataList.size()][1]; List<Beanclass> regList = new ArrayList<Beanclass>(); for (String[] strArray : dataList) { Beanclass bean = new Beanclass(); bean.setFirstName(strArray[0].trim()); bean.setLastName(strArray[1].trim()); bean.setPhoneNumber(strArray[2].trim()); bean.setUserName(strArray[3].trim()); bean.setCountry(strArray[4].trim()); bean.setEmail(strArray[5].trim()); bean.setPassword(strArray[6].trim()); bean.setConfirmPassword(strArray[7].trim()); regList.add(bean); } for (int i = 0; i < data.length; i++) { for (int j = 0; j < data[i].length; j++) { data[i][j] = regList.get(i); } } csvReader.close(); return data; } } ----------------------------------------------------------------------------- public class Formpage { private WebDriver driver; public Formpage(WebDriver driver){ this.driver = driver; } public Formpage enterfirstname(String name) { driver.findElement(By.name("firstName")).sendKeys(name); return this; } public Formpage enterlastname(String name) { driver.findElement(By.name("lastName")).sendKeys(name); return this; } public Formpage enterphone(String name) { driver.findElement(By.name("phone")).sendKeys(name); return this; } public Formpage enterusername(String name) { driver.findElement(By.name("userName")).sendKeys(name); return this; } public Formpage entercountry(String name) { new Select(driver.findElement(By.name("country"))) .selectByVisibleText(name); return this; } public Formpage enteremail(String name) { driver.findElement(By.name("email")).sendKeys(name); return this; } public Formpage enterpassword(String name) { driver.findElement(By.name("password")).sendKeys(name); return this; } public Formpage reenterpassword(String name) { driver.findElement(By.name("confirmPassword")).sendKeys(name); return this; } public Newpage clickregister() { driver.findElement(By.name("register")).click(); return new Newpage(driver); } public Newpage registeruser(Beanclass bean) { return enterfirstname(bean.getFirstName()) .enterlastname(bean.getLastName()) .enterphone(bean.getPhoneNumber()) .enterusername(bean.getUserName()) .entercountry(bean.getCountry()) .enteremail(bean.getEmail()) .enterpassword(bean.getPassword()) .reenterpassword(bean.getConfirmPassword()).clickregister(); } } |
Free forum by Nabble | Edit this page |