我通过Jenkins在CI流程中测试了在远程(docker上的硒网格)上的远程运行。测试将下载一个zip文件,然后我需要将其解压缩并从zip文件中验证内容。当我自己运行测试时(从我的Windows PC,仍处于远程状态,但没有docker),测试通过了,一切运行良好。但是当它在linux env中的CI上时,我没有成功获取文件路径
如您所见,“ get_file_names_from_selenium_grid”试图执行脚本,但是什么也没回来,我得到的是一个空列表,而不是包含一个项目的列表,该项目是zip文件的路径
def get_file_names_from_selenium_grid(self):
if not self.driver.current_url.startswith("chrome://downloads"):
self.driver.get("chrome://downloads/")
try:
for i in range(5):
paths = self.driver.execute_script(
"return downloads.Manager.get().items_.filter(e => e.state === 'COMPLETE').map(e => e.filePath || e.file_path || e.fileUrl || e.file_url); ")
if type(paths) is list and len(paths) > 0:
return paths
else:
time.sleep(5)
Assertion.add_message_soft_assert_arr("Didn't succeed to get file paths")
return False
except Exception as e:
Assertion.add_message_soft_assert_arr("Didn't succeed to get file paths" + str(e))
return False
def get_file_content_from_selenium_grid(self, path):
elem = self.driver.execute_script( \
"var input = window.document.createElement('INPUT'); "
"input.setAttribute('type', 'file'); "
"input.hidden = true; "
"input.onchange = function (e) { e.stopPropagation() }; "
"return window.document.documentElement.appendChild(input); ")
elem._execute('sendKeysToElement', {'value': [path], 'text': path})
result = self.driver.execute_async_script( \
"var input = arguments[0], callback = arguments[1]; "
"var reader = new FileReader(); "
"reader.onload = function (ev) { callback(reader.result) }; "
"reader.onerror = function (ex) { callback(ex.message) }; "
"reader.readAsDataURL(input.files[0]); "
"input.remove(); "
, elem)
if not result.startswith('data:'):
raise Exception("Failed to get file content: %s" % result)
return base64.b64decode(result[result.find('base64,') + 7:])
正如我说的,我得到一个空列表,然后我什么也没有发送到“ get_file_content_from_selenium_grid”。
最终以上代码成功了