unit testing - python selenium ide generated unittest, exporting using nose (xunit) with multiple cases using same browser -
so code below webdriver python code generated selenium ide on firefox. have other code generated long , wanted keep simple , short.
so problem want generate test case each line in test_ function need go in order, don't want create multiple test functions. have tried doing using yield functionality ( explained here http://nose.readthedocs.org/en/latest/writing_tests.html) doesn't passed loading web browser up. it's kinda hard explain since don't have actual code below.
i exporting results using nosetests --with-xunit , letting jenkins deal xml. when run code , @ xml, contains 1 large block of text output , thats in 1 tag, not separated.
so there way generate separate test cases still go through instructions sequentially. 1 solution have send in first instruction (driver.get(self.base_url + "/")), first 1 , second one, first, second, third. don't think best way.
also know assertions , of stuff unittest, need way have each web browser action test case not close browser when finishes each case, need keep 1 browser open , go through each action if played through selenium's ide on firefox.
maybe looking @ problem wrong, or coding wrong.
here generated code:
# -*- coding: utf-8 -*- selenium import webdriver selenium.webdriver.common.by import selenium.webdriver.common.keys import keys selenium.webdriver.support.ui import select selenium.common.exceptions import nosuchelementexception selenium.common.exceptions import noalertpresentexception import unittest, time, re class test(unittest.testcase): def setup(self): self.driver = webdriver.firefox() self.driver.implicitly_wait(30) self.base_url = "https://www.google.ca/" self.verificationerrors = [] self.accept_next_alert = true def test_(self): driver = self.driver driver.get(self.base_url + "/") driver.find_element_by_id("lst-ib").clear() driver.find_element_by_id("lst-ib").send_keys("nba") driver.find_element_by_link_text("nba.com").click() driver.find_element_by_link_text("tickets").click() driver.find_element_by_link_text("stats").click() def is_element_present(self, how, what): try: self.driver.find_element(by=how, value=what) except nosuchelementexception, e: return false return true def is_alert_present(self): try: self.driver.switch_to_alert() except noalertpresentexception, e: return false return true def close_alert_and_get_its_text(self): try: alert = self.driver.switch_to_alert() alert_text = alert.text if self.accept_next_alert: alert.accept() else: alert.dismiss() return alert_text finally: self.accept_next_alert = true def teardown(self): self.driver.quit() self.assertequal([], self.verificationerrors) if __name__ == "__main__": unittest.main()
here want (with pseudo code):
# -*- coding: utf-8 -*- selenium import webdriver selenium.webdriver.common.by import selenium.webdriver.common.keys import keys selenium.webdriver.support.ui import select selenium.common.exceptions import nosuchelementexception selenium.common.exceptions import noalertpresentexception import unittest, time, re class test(unittest.testcase): def setup(self): self.driver = webdriver.firefox() self.driver.implicitly_wait(30) self.base_url = "https://www.google.ca/" self.verificationerrors = [] self.accept_next_alert = true def test_(self): driver = self.driver #run instruction below , add list of tests driver.get(self.base_url + "/") #if previous instruction successful, run 1 below , add list driver.find_element_by_id("lst-ib").clear() #do same driver.find_element_by_id("lst-ib").send_keys("nba") #etc, idea...hopefully :) driver.find_element_by_link_text("nba.com").click() driver.find_element_by_link_text("tickets").click() driver.find_element_by_link_text("stats").click() def is_element_present(self, how, what): try: self.driver.find_element(by=how, value=what) except nosuchelementexception, e: return false return true def is_alert_present(self): try: self.driver.switch_to_alert() except noalertpresentexception, e: return false return true def close_alert_and_get_its_text(self): try: alert = self.driver.switch_to_alert() alert_text = alert.text if self.accept_next_alert: alert.accept() else: alert.dismiss() return alert_text finally: self.accept_next_alert = true def teardown(self): self.driver.quit() self.assertequal([], self.verificationerrors) if __name__ == "__main__": unittest.main()
the output if runs way want run:
website loaded google.ca ... ok element found lst-ib , cleared ... ok element found lst-ib , sent keys nba ... ok element found nba.com , clicked ... ok element found tickets , clicked ... ok element found stats , clicked ... ok ---------------------------------------------------------------------- ran 6 tests in 5.0s
currently looks this:
loaded google, searched , clicked nba.com, clicked tickets , starts... ok ---------------------------------------------------------------------- ran 1 tests in 5.0s
in xml 1 large block , doesn't good, nor give feedback on tests
thanks :)
--------update--------------
seems didn't explain problem enough
so want code this:
# -*- coding: utf-8 -*- selenium import webdriver selenium.webdriver.common.by import selenium.webdriver.common.keys import keys selenium.webdriver.support.ui import select selenium.common.exceptions import nosuchelementexception selenium.common.exceptions import noalertpresentexception import unittest, time, re class test(unittest.testcase): def setup(self): self.driver = webdriver.firefox() self.driver.implicitly_wait(30) self.base_url = "https://www.google.ca/" self.verificationerrors = [] self.accept_next_alert = true def test_one(self): driver = self.driver driver.get(self.base_url + "/") def test_two(self): driver = self.driver driver.find_element_by_id("lst-ib").clear() def test_three(self): driver = self.driver driver.find_element_by_id("lst-ib").send_keys("nba") def test_four(self): driver = self.driver driver.find_element_by_link_text("nba.com").click() def test_five(self): driver = self.driver driver.find_element_by_link_text("tickets").click() def test_six(self): driver = self.driver driver.find_element_by_link_text("stats").click() def is_element_present(self, how, what): try: self.driver.find_element(by=how, value=what) except nosuchelementexception, e: return false return true def is_alert_present(self): try: self.driver.switch_to_alert() except noalertpresentexception, e: return false return true def close_alert_and_get_its_text(self): try: alert = self.driver.switch_to_alert() alert_text = alert.text if self.accept_next_alert: alert.accept() else: alert.dismiss() return alert_text finally: self.accept_next_alert = true def teardown(self): self.driver.quit() self.assertequal([], self.verificationerrors) if __name__ == "__main__": unittest.main()
in situation above xml file created have 6 different entries, doesn't need since creates new browser every time starts test. need create 6 cases not close browser. thinking how remove setup test functions
Comments
Post a Comment