java - How to run multiple tests in one instance of driver -
i have following code, extracting data excel sheet , filling web form. registration form , registering 2 users. once second test runs, starts instance of driver. may know how in 1 instance.
@runwith(parameterized.class) public class form { private static webdriver driver; private string first; private string last; private string phone; private string country; private string about; public form(string first, string last, string phone, string country, string about) { this.first = first; this.last = last; this.phone = phone; this.country = country; this.about = about; } @before public void before() { driver = new firefoxdriver(); driver.manage().window().maximize(); } @parameters public static collection<object[]> supplydata() throws ioexception { file excel = new file("c:\\users\\master\\desktop\\search_query_log.xls"); fileinputstream fis = new fileinputstream(excel); hssfworkbook wb = new hssfworkbook(fis); hssfsheet ws = wb.getsheet("sheet1"); int rownum = ws.getlastrownum() + 1; int colnum = ws.getrow(0).getlastcellnum(); object[][] data = new object[rownum][colnum]; (int i=0; < rownum ; i++) { hssfrow row = ws.getrow(i); for(int j=0; j < colnum ; j++) { hssfcell cell = row.getcell((short) j); if(cell.getcelltype()==cell.cell_type_string) { data[i][j]=cell.getstringcellvalue(); } else if(cell.getcelltype()==cell.cell_type_numeric) { data[i][j]=string.valueof(cell.getnumericcellvalue()); } } } return arrays.aslist(data); } @test public void testcase() throws ioexception, interruptedexception { driver.get("http://www.samplereg.com"); driver.manage().timeouts().implicitlywait(10, timeunit.seconds); driver.findelement(by.id("fname")).sendkeys(first); driver.findelement(by.name("lastname")).sendkeys(last); driver.findelement(by.name("phonenumber")).sendkeys(phone); driver.manage().timeouts().implicitlywait(10, timeunit.seconds); webelement gender = driver.findelement(by.xpath("//input[@value='male']")); if (!gender.isselected()) gender.click(); asserttrue(gender.isselected()); driver.findelement(by.name("country")).sendkeys(country); driver.findelement(by.name("desc")).sendkeys(about); select industry = new select(driver.findelement(by.name("industry"))); assertfalse(industry.ismultiple()); assertequals(6, industry.getoptions().size()); list<string> exp_options = arrays.aslist(new string[]{"select industry", "it", "bpo","sales","development","other"}); list<string> act_options = new arraylist<string>(); for(webelement option : industry.getoptions()) act_options.add(option.gettext()); assertarrayequals(exp_options.toarray(),act_options.toarray()); assertequals("select industry", industry.getfirstselectedoption().gettext()); industry.selectbyvisibletext("bpo"); assertequals("bpo", industry.getfirstselectedoption().gettext()); select education = new select(driver.findelement(by.name("educationlist"))); assertfalse(education.ismultiple()); assertequals(4, education.getoptions().size()); list<string> exp_options1 = arrays.aslist(new string[]{"select education", "post graduate", "graduate", "under graduate"}); list<string> act_options1 = new arraylist<string>(); for(webelement option1 : education.getoptions()) act_options1.add(option1.gettext()); assertarrayequals(exp_options1.toarray(),act_options1.toarray()); assertequals("select education", education.getfirstselectedoption().gettext()); education.selectbyvisibletext("graduate"); assertequals("graduate", education.getfirstselectedoption().gettext()); webelement hobby = driver.findelement(by.xpath("//input[@value='listening music']")); if (!hobby.isselected()) hobby.click(); asserttrue(hobby.isselected()); driver.findelement(by.cssselector("input[type=file]")).sendkeys("c:\\users\\master\\desktop\\image.jpg"); driver.findelement(by.cssselector("input[type=submit]")).click(); } @after public void after() { //driver.quit(); } }
how trying below. hope understanding correct.
@beforeclass public static void setupclass() { driver = new firefoxdriver(); driver.manage().window().maximize(); alert javascriptalert = driver.switchto().alert(); system.out.println(javascriptalert.gettext()); // text on alert box javascriptalert.accept(); // chose whether accept or cancel based on need }
you can remove @before method.
Comments
Post a Comment