Selenium code to input values to text boxes from excel by opening browser single time -
i wrote selenium code read values excel sheet contains 2 columns.i need input these 2 text boxes in webpage .i not want use testng @ point .my issue every time browser opens webpage each entry ie each row . have 10 rows in excel sheet 10 web pages opened , each input text boxes .how can rewrite code open single browser , input values 1 one .
public class insert { public static void main(string[] args) { try { fileinputstream fis = new fileinputstream("f:\\book4.xlsx"); xssfworkbook wb = new xssfworkbook(fis); xssfsheet sheet = wb.getsheet("testdata"); for(int count = 1;count<=sheet.getlastrownum();count++) { xssfrow row = sheet.getrow(count); system.out.println("running test case " + row.getcell(0).tostring()); runtest(row.getcell(1).tostring(),row.getcell(2).tostring()); } fis.close(); } catch (ioexception e) { system.out.println("test data file not found"); } } public static void runtest(string name,string mailid) { webdriver driver=new firefoxdriver(); driver.get("http://www.deal4loans.com/"); driver.manage().timeouts().implicitlywait(10, timeunit.seconds); driver.findelement(by.xpath("html/body/div[4]/div/div/div[1]/a[1]")).click(); driver.manage().timeouts().implicitlywait(10, timeunit.seconds); driver.findelement(by.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[1]/td[2]/input")).sendkeys(name); driver.findelement(by.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/input")).sendkeys(mailid); } }
i assuming data below ('--' used separator columns):
name -- email-id name1 -- www.email.com1 name2 -- www.email.com2 name3 -- www.email.com3 name4 -- www.email.com4 name5 -- www.email.com5 name6 -- www.email.com6
below code navigates browser once , enters in fields @ interval of 2 seconds.
[works fine when tried xls file "hssfworkbook" class]
public class insert { static webdriver driver; public static void main(string[] args) throws interruptedexception { driver=new firefoxdriver(); driver.manage().window().maximize(); driver.manage().timeouts().implicitlywait(20, timeunit.seconds); driver.get("http://www.deal4loans.com/"); driver.findelement(by.xpath("html/body/div[4]/div/div/div[1]/a[1]")).click(); try { fileinputstream fis = new fileinputstream("f:\\book4.xlsx"); xssfworkbook wb = new hssfworkbook(fis); xssfsheet sheet = wb.getsheet("testdata"); for(int count=1;count<=sheet.getlastrownum();count++) { xssfrow row = sheet.getrow(count); system.out.println("\n----------------------------"); system.out.println("running test case " + count); runtest(row.getcell(0).tostring(),row.getcell(1).tostring()); } fis.close(); driver.close();// closing firefox driver instance } catch (ioexception e) { system.out.println("test data file not found"); } } public static void runtest(string name,string mailid) throws interruptedexception { system.out.println("inputing name: "+name+" , mailid: "+mailid); driver.findelement(by.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[1]/td[2]/input")).clear(); driver.findelement(by.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/input")).clear(); driver.findelement(by.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[1]/td[2]/input")).sendkeys(name); driver.findelement(by.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/input")).sendkeys(mailid); system.out.println("inputted name: "+name+" , mailid: "+mailid); thread.sleep(2000); // sleeping 2 seconds each entry detected. } }
Comments
Post a Comment