SuperB SuperB - 1 month ago 11
Python Question

Editing Selenium (python) exported test case to read data from a text file

I have exported a test case and saved as .py file. I want to edit it to read data from a text file, as inputs for logging into the website that I am testing. The sample username and password that should allow a login is, 1 and pwd1 respectively. But when I read these values from a file by accessing the first line read , using readlines(), It shows failure while logging in and the test case fails. When I printed the size of the corresponding line read by readlines, it's size is 1 more than it should be. i.e. size=2 whereas size of my username, 1, should be 1 I assume. Need help in making this test case a success. Here is my code:

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
import time

class A6Test2(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.base_url = "http://localhost:8080"
self.verificationErrors = []
self.accept_next_alert = True

def test_a6_test2(self):
fh = open("cases.txt", "r")
data=fh.readlines()
uname1=data[1]
pwd1=data[2]
driver = self.driver
driver.get(self.base_url + data[0])
driver.find_element_by_name("p_id").clear()
driver.find_element_by_name("p_id").send_keys(data[1])
driver.find_element_by_name("p_passw").clear()
driver.find_element_by_name("p_passw").send_keys(data[2])
time.sleep(5)
driver.find_element_by_xpath("(//input[@value='Submit'])[2]").click()
time.sleep(3)
driver.find_element_by_css_selector("input[type=\"SUBMIT\"]").click()
driver.find_element_by_link_text(data[5]).click()
driver.get(self.base_url + data[6])
time.sleep(3)
driver.find_element_by_name("p_id").clear()
driver.find_element_by_name("p_id").send_keys(data[7])
time.sleep(3)
driver.find_element_by_name("p_passw").clear()
driver.find_element_by_name("p_passw").send_keys(data[8])
time.sleep(3)
driver.find_element_by_xpath("(//input[@value='Submit'])[2]").click()
time.sleep(3)
driver.find_element_by_css_selector("input[type=\"SUBMIT\"]").click()
driver.find_element_by_link_text(data[11]).click()

def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what)
except NoSuchElementException as e: return False
return True

def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException as 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()


My cases.txt is as follows:

/source.jsp

1

pwd1

(//input[@value='Submit'])[2]

input[type=\"SUBMIT\"]

Log out

/source.jsp

1

pwd1

(//input[@value='Submit'])[2]

input[type=\"SUBMIT\"]

Log out

Answer

data can be seen as a 2D array. Try accessing it using 2 indices For Example:

driver.get(self.base_url + "/source.jsp")
driver.find_element_by_name("d_id").clear()
driver.find_element_by_name("d_id").send_keys(data[0][0:3])
driver.find_element_by_name("d_passw").clear()
driver.find_element_by_name("d_passw").send_keys(data[1][0:5])

The above is assuming some fixed lengths of your id and password.

But if you do not want to give predefined values like 3 and 5 as above, try using the length function.