请参考以下代码使用 Python 和 Selenium 测试亚马逊搜索引擎的单字符输入:-
只需创建一个包含 get-row-count、get_col_count、read-data 和 write_data 方法的 XLUtils 模块。
导入所有模块,您将在 SingleCharSearchData.xlx 中获得 Number of Suggestions 和 Results 列
享受!!!!
import unittest
from pageObjects.config import Configg
from pageObjects.loginPage import Login
from pageObjects.searchpage import Search
import time
from testcases import XLUtils
class SearchTest(unittest.TestCase):
def test_single_char_search_suggestions(self):
conf = Configg(self)
conf.driver_setup()
login = Login(conf.driver)
login.get_url()
search = Search(conf.driver)
search.click_search_bar()
path = "C://Users//user//Amazon//Test Data//SingCharSearchData.xlsx"
row = XLUtils.get_row_count(path, "Sheet1")
print(row)
for r in range(2, row+1):
all_suggestion = {}
single_char = XLUtils.read_data(path, "Sheet1", r, 1)
conf.driver.find_element_by_xpath\
("//*[@id='twotabsearchtextbox']").send_keys(single_char)
time.sleep(2)
suggestions = conf.driver.find_element_by_id("suggestions").text
all_suggestion[single_char] = suggestions
conf.driver.find_element_by_xpath("//*
[@id='twotabsearchtextbox']").clear()
for key in all_suggestion:
all_suggestion_list = all_suggestion[key].split('\n')
all_suggestion_list_v2 = []
for item in all_suggestion_list:
if item.startswith(key) and not item.startswith("in"):
all_suggestion_list_v2.append(item)
if len(all_suggestion_list_v2) == 10 or len(all_suggestion_list_v2) <
10:
print("Test Passed.")
XLUtils.write_data(path, "Sheet1", r, 2,
len(all_suggestion_list_v2))
XLUtils.write_data(path, "Sheet1", r, 3, "Test Passed")
else:
print("Test Failed.")
XLUtils.write_data(path, "Sheet1", r, 2,
len(all_suggestion_list_v2))
XLUtils.write_data(path, "Sheet1", r, 3, "Test Failed")
显然,我们可以对此进行更多优化,如果我这样做了,我也会发布该代码。
谢谢
戈文德库马尔