"""Browser acceptance for catalog navigation, routing, and refresh status.""" import json,pathlib,urllib.request from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.firefox.options import Options from selenium.webdriver.firefox.service import Service from selenium.webdriver.support.ui import WebDriverWait,Select BASE='http://127.0.0.1:8088';ROOT=pathlib.Path(__file__).resolve().parents[1];TARGET='dobot_xtrainer-软件版本区别' options=Options();options.add_argument('--headless');options.binary_location='/snap/firefox/current/usr/lib/firefox/firefox' driver=webdriver.Firefox(options=options,service=Service('/snap/bin/geckodriver'));driver.set_window_size(1440,1000);wait=WebDriverWait(driver,30,poll_frequency=.1) def body():return driver.find_element(By.TAG_NAME,'body').text def source(name):driver.find_element(By.XPATH,f"//aside[@aria-label='内容来源']//button[.//b[.='{name}']]").click();wait.until(lambda d:d.find_element(By.CSS_SELECTOR,'.catalog-summary strong').text==name) def folder(name):driver.find_element(By.XPATH,f"//button[contains(@class,'catalog-name') and contains(.,'{name}')]").click() def fits():assert driver.execute_script('return document.documentElement.scrollWidth<=window.innerWidth') try: driver.get(BASE);wait.until(lambda d:d.find_element(By.TAG_NAME,'h1').text=='来源与目录');wait.until(lambda d:len(d.find_elements(By.CSS_SELECTOR,'.catalog-source'))==7);wait.until(lambda d:d.find_elements(By.CSS_SELECTOR,'.snapshot-timestamp')) assert '实时刷新目录' in body() and '快照时间' in body() assert '实时快照已完成' in body() and '实时快照完成:1079 个目录,3444 份文件' in body() folder('02 产品资料');wait.until(lambda d:'返回上一级' in body());driver.find_element(By.XPATH,"//button[contains(.,'返回上一级')]").click();wait.until(lambda d:len(d.find_elements(By.CSS_SELECTOR,'.catalog-table tbody tr'))==8) source('PP资料下载中心');folder('具身产品');wait.until(lambda d:'Dobot X-Trainer' in body());folder('Dobot X-Trainer');wait.until(lambda d:TARGET in body()) operator=driver.find_element(By.CSS_SELECTOR,'.routing-fields input[placeholder="姓名或工号"]');operator.send_keys('browser-routing') reason=driver.find_elements(By.CSS_SELECTOR,'.routing-fields input')[1];reason.clear();reason.send_keys('网页单条与批量分流验收') row=driver.find_element(By.XPATH,f"//tr[.//*[contains(.,'{TARGET}')]]");choice=Select(row.find_element(By.TAG_NAME,'select'));choice.select_by_value('external');wait.until(lambda d:'已标注 1 份' in body() and '已在目标队列 1 份' in body()) wait.until(lambda d:'对外' in d.find_element(By.XPATH,f"//tr[.//*[contains(.,'{TARGET}')]]/td[3]").text) # Current-page bulk selection is idempotent and should not create another publication. row=driver.find_element(By.XPATH,f"//tr[.//*[contains(.,'{TARGET}')]]");row.find_element(By.CSS_SELECTOR,'input[type="checkbox"]').click();driver.find_element(By.XPATH,"//button[.='批量标注对外并入队']").click();wait.until(lambda d:'已在目标队列 1 份' in body()) assert '新入队 0 份' in body() for width in (1440,1024,768):driver.set_window_size(width,900);fits() driver.set_window_size(1440,1000);driver.get_full_page_screenshot_as_file(str(ROOT/'tmp/CATALOG_ROUTING_REFRESH_FIREFOX.png')) result={'back':'pass','snapshot_time':'pass','refresh_completed':'pass','single_route':'pass','bulk_route':'pass','idempotent':True,'final_visibility':'external','root_overflow':False} (ROOT/'tmp/catalog-routing-refresh-acceptance.json').write_text(json.dumps(result,ensure_ascii=False,indent=2));print(json.dumps(result,ensure_ascii=False)) finally:driver.quit()