【问题标题】:Test missed in selenium using python使用 python 在 selenium 中错过了测试
【发布时间】:2017-08-09 20:00:20
【问题描述】:

我正在尝试测试网站live.guru99.com。我正在测试三个功能,名为测试 A、B 和 C。我正在使用 geckodriver 测试代码,其 exe 位于根文件夹中。

代码如下:

from selenium import webdriver
import time
import unittest

class Guru99BankTest(unittest.TestCase):

    @classmethod
    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_C(self):
        driver = self.driver
        driver.get("http://live.guru99.com/")

        driver.find_element_by_link_text('Mobile').click()

        listed_prd_el = driver.find_element_by_xpath(
            "//a[contains(text(), 'Sony Xperia')]/ancestor::div[@class = 'product-info']")
        listed_prd_el.find_element_by_tag_name('button').click()

        qty = driver.find_element_by_xpath("//input[@title='Qty']")

        qty.clear()
        qty.send_keys('1000')

        self.assertEqual(qty.get_attribute('value'), '1000')

        driver.find_element_by_xpath("//button[@title='Update']").click()

        time.sleep(3)
        self.assertIn('products cannot be ordered in requested quantity',
                      driver.find_element_by_class_name('error-msg').text)

        driver.find_element_by_xpath("//button[@title='Empty Cart']").click()

        time.sleep(3)

        self.assertIn('no items in your shopping cart',
                      driver.find_element_by_class_name('cart-empty').text)

    def test_A(self):
        driver = self.driver

        driver.get("http://live.guru99.com/")

        self.assertIn("This is demo site for", driver.page_source)

        driver.find_element_by_link_text('Mobile').click()

        self.assertEqual("Mobile", driver.title)

    def test_B(self):
        driver = self.driver

        driver.get("http://live.guru99.com/")

        driver.find_element_by_link_text('Mobile').click()

        driver.find_element_by_xpath("//select[@title='Sort By']/option[contains(text(), 'Name')]").click()

        product_names = ([e.text for e in driver.find_elements_by_xpath("//h2[@class='product-name']")])

        self.assertEqual(product_names, sorted(product_names))

        listed_prd_el = driver.find_element_by_xpath(
            "//a[contains(text(), 'Sony Xperia')]/ancestor::div[@class = 'product-info']")
        listed_price = listed_prd_el.find_element_by_class_name("price").text

        listed_prd_el.find_element_by_tag_name('a').click()

        prod_price = driver.find_element_by_xpath('//span[@class="price"]').text

        self.assertEqual(listed_price, prod_price)



    @classmethod
    def tearDown(self):
        self.driver.close()

def custom_suite():
    suite = unittest.TestSuite()
    suite.addTest(Guru99BankTest('test_A'))
    suite.addTest(Guru99BankTest('test_B'))
    suite.addTest(Guru99BankTest('test_C'))
    return suite

if __name__ == "__main__":
    runner = unittest.TextTestRunner()
    runner.run(custom_suite())

但是,我发现浏览器只打开一次而不是三次。我做错了什么?我是单元测试的新手,所以请多多包涵。

【问题讨论】:

  • 有两个测试同名test_B,是故意的吗?
  • @michaelsatish 很抱歉打错了

标签: python selenium python-unittest


【解决方案1】:

它对我有用。我将最后一个测试更改为 test_C 因为 test_b 被复制了。不过话说回来我也不确定是不是实际情况

【讨论】:

  • 我刚刚在我的控制台中进行了一次运行 1 测试
  • 尝试在 chrome 中运行它,不确定它是否会产生任何影响,我希望 chrome 驱动程序在附近,所以在 Chrome 中触发了您的代码并正常工作。但我不认为浏览器类型和实例决定了流程
猜你喜欢
  • 1970-01-01
  • 2014-06-05
  • 2011-04-24
  • 2017-05-26
  • 2021-09-06
  • 1970-01-01
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多