如何从用于selenium测试的ini文件中获取价值?

时间:2017-04-03 17:45:01

标签: python selenium

在java之后我尝试使用python创建一个使用selenium的简单登录测试。 所以在我的项目中,我创建了test.ini,其中包含: [登录] username = tester@myapp.com

我的python测试文件是:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
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
from web import*

class Login(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome("C://Selenium-driver//chromedriver.exe")
        self.driver.implicitly_wait(30)
        self.base_url = "http://myapp.com/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_x(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        timestr = time.strftime('%Y%m%d-%H%M%S')
        time.sleep(30)

    try:
        from configparser import ConfigParser
    except ImportError:
        from ConfigParser import ConfigParser  # ver. < 3.0
             # instantiate
        config = ConfigParser()
             # parse existing file
        config.read('test.ini')
             # read values from a section
        string_val = config.get('login', 'username')    

        driver.find_element_by_xpath("(//input[@id='input'])[2]").click()
        driver.find_element_by_xpath("(//input[@id='input'])[2]").clear()
        driver.find_element_by_xpath("(//input[@id='input'])[2]").send_keys(string_val)

    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()

因此,当我运行python测试时,用户名永远不会从ini文件中使用。 浏览器以url开头,然后自行关闭而不会出错。

为什么我使用ini文件?因为我想为网页元素创建类似class repository的内容,但它对我不起作用。 所以在谷歌搜索后,看起来我可以使用ini文件来存储所有的网页元素,然后在我的测试中使用。

请有人帮助我。 谢谢

0 个答案:

没有答案