Feb-11-2018, 04:48 PM
Hi there, I'm trying to refactor my code as at the moment it is far too clunky for what I am trying to achieve. I read in a line at a time of an excel spreadsheet and use the data from it to enter into Web Pages with Selenium Webdriver. The problem I'm having is literally all of my functional code (selenium code etc.) has to be INSIDE the 'for' loop (which loops through the spreadsheet). This as you can see is a really bad way to get what I need it to do, but I literally can't think of another way to read in a line, do some selenium stuff, then read in the next row. I also have an if statement in it as a "run row" for yes or no in the first excel cell.
Is there a way I can have a very small 'for' loop where I basically run a test i.e. call another module for instance with the selenium code in it? Or failing that, staying in the same module but running the selenium code outside of the 'for' loop?
BTW in the code below "data" is the list of dictionaries of the excel sheet. Basically a list of each row of the spreadsheet. (Also there is an obvious indent issue on a long line of selenium code but I couldn't figure it out on this forum.)
I hope it makes sense! Many thanks. :)
Is there a way I can have a very small 'for' loop where I basically run a test i.e. call another module for instance with the selenium code in it? Or failing that, staying in the same module but running the selenium code outside of the 'for' loop?
BTW in the code below "data" is the list of dictionaries of the excel sheet. Basically a list of each row of the spreadsheet. (Also there is an obvious indent issue on a long line of selenium code but I couldn't figure it out on this forum.)
I hope it makes sense! Many thanks. :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
"""Module where I read the excel sheet and put in 'data'""" from read_sheet import * """this is my selenium webdriver module""" import driver_global class runUnitTest_class(driver_global.SeleniumTest): """loop through the list""" for i in range ( len (data)): if data[i][ 'Run' ] = = 'Y' or data[i][ 'Run' ] = = 'y' : print "run the below..." url_value = data[i][ 'URL' ] password_value = data[i][ 'password' ] username_value = data[i][ 'username' ] print url_value print password_value print username_value """Selenium coding below. Need to refactor""" driver_global.SeleniumTest.setUpClass() driver_global.SeleniumTest.driver.get(url_value) driver_global.SeleniumTest.driver.find_element_by_id( "username" ).clear() driver_global.SeleniumTest.driver.find_element_by_id( "username" ).send_keys(username_value) driver_global.SeleniumTest.driver.find_element_by_id( "password" ).clear() driver_global.SeleniumTest.driver.find_element_by_id( "password" ).send_keys(password_value) driver_global.SeleniumTest.driver.find_element_by_css_selector( "button.button" ).click() driver_global.SeleniumTest.driver.find_element_by_xpath( "//header[@id='header']/div/nav/div/div/a[8]/div" ).click() driver_global.SeleniumTest.take_screenshot_now( "from_runUnitTest" ) driver_global.SeleniumTest.tearDownClass() elif data[i][ 'Run' ] = = 'N' or data[i][ 'Run' ] = = 'n' : print "Don't run row " + str (i + 1 ) elif data[i][ 'Run' ] ! = 'N' or data[i][ 'Run' ] ! = 'n' or data[i][ 'Run' ] ! = 'Y' or data[i][ 'Run' ] ! = 'y' : print "enter something useful: Y or N" else : pass |