如何在Selenium测试中安装Chrome扩展程序?

时间:2015-02-22 11:26:47

标签: python selenium google-chrome-extension

我们有Chrome扩展程序,我们希望使用Selenium进行测试。用户通过单击我们网站上的按钮(登录或创建签名后)安装我们的扩展程序。我创建了一个Selenium测试,但我不知道如何确认"添加"安装扩展时的对话框。这是我的代码:

import sys
sys.path.insert(0, 'libs')

import unittest
from selenium import webdriver


class SeleniumTests(unittest.TestCase):
    def setUp(self):
        caps = {}

        caps['name'] = 'Chrome Inbox Test'
        caps['build'] = '1.0'
        caps['browser_api_name'] = 'Chrome40x64'
        caps['os_api_name'] = 'Win8.1'
        caps['screen_resolution'] = '1280x1024'
        caps['record_video'] = 'true'
        caps['record_network'] = 'true'
        caps['record_snapshot'] = 'true'

        self.driver = webdriver.Remote(
            desired_capabilities=caps,
            command_executor="http://[username]:[password]@hub.crossbrowsertesting.com:80/wd/hub"
        )

        self.driver.implicitly_wait(time_to_wait=10)

    def login_to_webapp(self):
        self.driver.get(url='http://example.com/logout')
        self.driver.maximize_window()
        self.assertEqual(first="Web Editor", second=self.driver.title)
        action = webdriver.ActionChains(driver=self.driver)
        action.move_to_element(to_element=self.driver.find_element_by_xpath(xpath="//div[@id='header_floater']/div[@class='header_menu']/button[@class='btn_header signature_menu'][text()='My signature']"))
        action.perform()
        self.driver.find_element_by_xpath(xpath="//ul[@id='signature_menu_downlist'][@class='menu_downlist']/li[text()='Log In']").click()
        self.driver.find_element_by_xpath(xpath="//form[@id='atho-form']/div[@class='input']/input[@name='useremail']").send_keys("[email]")
        self.driver.find_element_by_xpath(xpath="//form[@id='atho-form']/div[@class='input']/input[@name='password']").send_keys("[password]")
        self.driver.find_element_by_xpath(xpath="//form[@id='atho-form']/button[@type='submit'][@class='atho-button signin_button'][text()='Sign in']").click()

    def install_chrome_extension(self):
        self.assertEqual(first=0, second=len(self.driver.find_elements_by_xpath(xpath="//div[@id='emails_overlay'][contains(@style,'display: block;')]/div[@id='emails_pick'][contains(@style,'display: block;')]")))
        self.driver.find_element_by_xpath(xpath="//button[@id='save']/span[@class='no-extension'][text()=\"OK, I'm Done\"]").click()
        # Add extension to Chrome.
        self.assertEqual(first=1, second=len(self.driver.find_elements_by_xpath(xpath="//div[@id='emails_overlay'][contains(@style,'display: block;')]/div[@id='emails_pick'][contains(@style,'display: block;')]"))) # Will be true only if the extension is installed.

    def test_chrome_inbox(self):
        self.login_to_webapp()
        self.install_chrome_extension()

    def tearDown(self):
        print("Done with session %s" % self.driver.session_id)
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()

点击"好的,我完成了"使用Selenium,我想确认将扩展名添加到Chrome(会显示一个对话框,其中包含"添加"或"取消",我想点击"添加")。我该怎么做?

如果无法做到这一点,我可以直接从Chrome网上应用店安装扩展程序。但是如果可能的话,我更愿意测试从我们的网站安装扩展程序。

0 个答案:

没有答案