1919import os
2020import pytest
2121
22- EXTENSION_ID = "[email protected] " 23- EXTENSION_PATH = "common/extensions/webextensions-selenium-example-signed"
24- EXTENSION_ARCHIVE_PATH = "common/extensions/webextensions-selenium-example.xpi"
25-
26-
27- # Function to find the project root directory
28- def find_project_root ():
29- current_dir = os .path .dirname (os .path .abspath (__file__ ))
30-
31- while current_dir != os .path .dirname (current_dir ):
32- extensions_dir = os .path .join (current_dir , "common" , "extensions" )
33- if os .path .isdir (extensions_dir ):
34- return current_dir
35- current_dir = os .path .dirname (current_dir )
22+ from selenium .webdriver .common .by import By
23+ from selenium .webdriver .support .wait import WebDriverWait
3624
37- return os .path .dirname (os .path .abspath (__file__ ))
3825
26+ EXTENSION_ID = "[email protected] " 27+ EXTENSION_PATH = "webextensions-selenium-example"
28+ EXTENSION_ARCHIVE_PATH = "webextensions-selenium-example.xpi"
3929
40- @pytest .fixture
41- def locate_project_path ():
42- """Locate the project path for the extension files."""
43- project_root = find_project_root ()
44- return project_root
30+ extensions = os .path .abspath ("../../../../../../test/extensions/" )
4531
4632
4733def test_webextension_initialized (driver ):
@@ -51,52 +37,114 @@ def test_webextension_initialized(driver):
5137
5238@pytest .mark .xfail_chrome
5339@pytest .mark .xfail_edge
54- def test_install_extension_path (driver , locate_project_path ):
40+ def test_install_extension_path (driver , pages ):
5541 """Test installing an extension from a directory path."""
56- path = os .path .join (locate_project_path , EXTENSION_PATH )
42+ path = os .path .join (extensions , EXTENSION_PATH )
5743
5844 ex_in = driver .webextension .install (path = path )
5945 assert ex_in .get ("extension" ) == EXTENSION_ID
6046
47+ pages .load ("blank.html" )
48+ injected = WebDriverWait (driver , timeout = 2 ).until (
49+ lambda dr : dr .find_element (By .ID , "webextensions-selenium-example" )
50+ )
51+ assert injected .text == "Content injected by webextensions-selenium-example"
52+
6153 driver .webextension .uninstall (ex_in )
6254
55+ context_id = driver .current_window_handle
56+ driver .browsing_context .reload (context_id )
57+ assert len (driver .find_elements (By .ID , "webextensions-selenium-example" )) == 0
58+
6359
6460@pytest .mark .xfail_chrome
6561@pytest .mark .xfail_edge
66- def test_install_archive_extension_path (driver , locate_project_path ):
62+ def test_install_archive_extension_path (driver , pages ):
6763 """Test installing an extension from an archive path."""
68- path = os .path .join (locate_project_path , EXTENSION_ARCHIVE_PATH )
64+ path = os .path .join (extensions , EXTENSION_ARCHIVE_PATH )
6965
7066 ex = driver .webextension .install (archive_path = path )
7167 assert ex .get ("extension" ) == EXTENSION_ID
7268
69+ pages .load ("blank.html" )
70+ injected = WebDriverWait (driver , timeout = 2 ).until (
71+ lambda dr : dr .find_element (By .ID , "webextensions-selenium-example" )
72+ )
73+ assert injected .text == "Content injected by webextensions-selenium-example"
74+
7375 driver .webextension .uninstall (ex )
7476
77+ context_id = driver .current_window_handle
78+ driver .browsing_context .reload (context_id )
79+
80+ assert len (driver .find_elements (By .ID , "webextensions-selenium-example" )) == 0
81+
7582
7683@pytest .mark .xfail_chrome
7784@pytest .mark .xfail_edge
78- def test_install_base64_extension_path (driver , locate_project_path ):
85+ def test_install_base64_extension_path (driver , pages ):
7986 """Test installing an extension from a base64 encoded string."""
80- path = os .path .join (locate_project_path , EXTENSION_ARCHIVE_PATH )
87+ path = os .path .join (extensions , EXTENSION_ARCHIVE_PATH )
8188
8289 with open (path , "rb" ) as file :
8390 base64_encoded = base64 .b64encode (file .read ()).decode ("utf-8" )
8491
8592 ex = driver .webextension .install (base64_value = base64_encoded )
8693 assert ex .get ("extension" ) == EXTENSION_ID
8794
95+ pages .load ("blank.html" )
96+ injected = WebDriverWait (driver , timeout = 2 ).until (
97+ lambda dr : dr .find_element (By .ID , "webextensions-selenium-example" )
98+ )
99+ assert injected .text == "Content injected by webextensions-selenium-example"
100+
101+ driver .webextension .uninstall (ex )
102+
103+ context_id = driver .current_window_handle
104+ driver .browsing_context .reload (context_id )
105+
106+ assert len (driver .find_elements (By .ID , "webextensions-selenium-example" )) == 0
107+
108+
109+ @pytest .mark .xfail_chrome
110+ @pytest .mark .xfail_edge
111+ def test_install_unsigned_extension (driver , pages ):
112+ """Test installing an unsigned extension."""
113+ path = os .path .join (extensions , "webextensions-selenium-example-unsigned" )
114+
115+ ex = driver .webextension .install (path = path )
116+ assert ex .get ("extension" ) == EXTENSION_ID
117+
118+ pages .load ("blank.html" )
119+ injected = WebDriverWait (driver , timeout = 2 ).until (
120+ lambda dr : dr .find_element (By .ID , "webextensions-selenium-example" )
121+ )
122+ assert injected .text == "Content injected by webextensions-selenium-example"
123+
88124 driver .webextension .uninstall (ex )
89125
126+ context_id = driver .current_window_handle
127+ driver .browsing_context .reload (context_id )
128+
129+ assert len (driver .find_elements (By .ID , "webextensions-selenium-example" )) == 0
130+
90131
91132@pytest .mark .xfail_chrome
92133@pytest .mark .xfail_edge
93- def test_install_with_extension_id_uninstall (driver , locate_project_path ):
134+ def test_install_with_extension_id_uninstall (driver , pages ):
94135 """Test uninstalling an extension using just the extension ID."""
95- path = os .path .join (locate_project_path , EXTENSION_PATH )
136+ path = os .path .join (extensions , EXTENSION_PATH )
96137
97138 ex = driver .webextension .install (path = path )
98139 extension_id = ex .get ("extension" )
99140 assert extension_id == EXTENSION_ID
100141
142+ pages .load ("blank.html" )
143+
101144 # Uninstall using the extension ID
102145 driver .webextension .uninstall (extension_id )
146+
147+ context_id = driver .current_window_handle
148+ driver .browsing_context .reload (context_id )
149+
150+ assert len (driver .find_elements (By .ID , "webextensions-selenium-example" )) == 0
0 commit comments