当前位置:网站首页>自动答题 之 Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。
自动答题 之 Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。
2022-07-06 09:33:00 【心蓝168】
前几天接到的小单子,某些人员为了应付单位的硬性要求,每天必须在网上学习多少道题,每天必须在网上练习多少道题,所以想找人开发一个自动答题系统。上图为《某练兵比武在线学习》的自动答题演示。
怀着好奇的心里,在网上找找自动答题软件的实现方法,本想着使用IFrame,嵌入答题网页,使用jquery对网页进行操作,但后来发现打开答题窗口时,网页使用了target=_top,所以该方法又走不通了,只有接着百度溜达溜达,大概花了2小时左右, 自动化测试工具,即:Selenium,可以直接通过浏览器进行操作。
这也是这几天和同事一起讨论自动系统巡检,检测系统的运行状态,这过程中,发现同事写的Demo也是自动,看到浏览器上东西不用鼠标操作,就能动起来,实在好玩。
"Selenium测试直接运行在浏览器中,就像真正的用户在操作一样"
官方网站为:http://www.selenium.org.cn/
具体的C#实现过程如下:
//日日学-自动答题
private void AutoAnswerTitleDay()
{
try
{
if (DriverObj.WindowHandles.Count <= 1)
{
this.OutAnswerLog("请确保【日日学】的学习页面已打开...");
return;
}
DriverObj.SwitchTo().Window(DriverObj.WindowHandles[1]);
this.OutAnswerLog(string.Format("第 {0} 题等待:{1} 秒", (this.CountNumDay + 1).ToString().PadLeft(3, ' '), 0));
List<IWebElement> tabGroupList = DriverObj.FindElements(By.ClassName("question_tab_cui")).ToList();
if (tabGroupList.Count == 0)
{
return;
}
this.LearnCount = (new Random()).Next(31, 41);
List<IWebElement> tabItemsList = tabGroupList[0].FindElements(By.TagName("LI")).ToList();
foreach (IWebElement tabItem in tabItemsList)
{
//获取题类
string tabTypeName = tabItem.Text;
string titleTypeTx = string.Empty;
foreach (string typeItem in this.TitleTypeList)
{
if (tabTypeName.IndexOf(typeItem) > -1)
{
titleTypeTx = typeItem;
break;
}
}
if (titleTypeTx.Length == 0)
{
break;
}
//跳出学习
if (this.CountNumDay >= this.LearnCount)
{
break;
}
//切换题类
this.MoveMouseToPoint(tabItem);
this.ElementClickEvent(tabItem);
Thread.Sleep(1000);
while (true)
{
try
{
//题目类型
string titleType = this.ReturnTitleType(titleTypeTx + "题");
//获取题目
string titleText = this.ReturnTitleContentDay();
if (titleText.Length == 0)
{
this.RecordSystemLog("无法获取题目");
break;
}
//跳出学习
if (this.CountNumDay >= this.LearnCount)
{
this.RecordSystemLog("已达到学习数量");
break;
}
//获取选项
List<string> optionTextList = this.ReturnTitleOptionDay();
List<string> answerItemList = this.FindTitleAnswer(titleType, titleTypeTx, titleText);
//填写答案
if ("单选|多选|判断".IndexOf(titleTypeTx) > -1)
{
this.SelectTitleAnswerDay(answerItemList, titleTypeTx);
}
if ("填空".IndexOf(titleTypeTx) > -1)
{
this.WriteTitleAnswerTKDay(answerItemList[0]);
}
if ("简答|案例".IndexOf(titleTypeTx) > -1)
{
this.WriteTitleAnswerWDDay(answerItemList[0]);
}
//打开下题
this.CountNumDay++;
this.GotoNextTitleDay(titleTypeTx);
}
catch (Exception xe)
{
this.RecordSystemLog("日日学答题时发生错误-B:" + xe.Message);
}
}
}
}
catch (Exception xe)
{
this.RecordSystemLog("日日学答题时发生错误-A:" + xe.Message);
}
}
//日日学-回调函数
private void SelectTitleAnswerDayCallBack(IAsyncResult ar)
{
this.PanelTools.Enabled = true;
this.OutAnswerLog("本次答题结束,共学习了 " + this.CountNumDay + "题" + (this.CountNumDay < this.LearnCount ? ",题量不足,请重新学习。" : "。"));
MainForm.FlashWindow(this.Handle, true);
}
//日日学-选择答案(单选,多选,判断)
private void SelectTitleAnswerDay(List<string> answerItemList, string titleType)
{
List<IWebElement> groupList = DriverObj.FindElements(By.ClassName("option_fl")).ToList();
if (groupList.Count == 0)
{
return;
}
List<IWebElement> optionsList = groupList[0].FindElements(By.TagName("LI")).ToList();
bool isSelectAnswer = false;
foreach (var optionItem in optionsList)
{
IWebElement optionLink = optionItem.FindElement(By.TagName("A"));
string optionItemName = optionItem.Text.Trim().Replace("\r\n", string.Empty);
string optionLinkName = optionLink.Text.Trim().Replace("\r\n", string.Empty);
if (optionLinkName.Length > 0)
{
//单选|多选
if (answerItemList.Exists(c => c == optionItemName.Substring(optionLinkName.Length)) == true)
{
isSelectAnswer = true;
this.MoveMouseToPoint(optionLink);
this.ElementClickEvent(optionLink);
}
}
else
{
//判断
if (answerItemList.Exists(c => c == optionItemName) == true)
{
isSelectAnswer = true;
this.MoveMouseToPoint(optionLink);
this.ElementClickEvent(optionLink);
}
}
}
if (isSelectAnswer == false)
{
Random randObj = new Random();
if (titleType == "单选" || titleType == "判断")
{
IWebElement optionLink = optionsList[randObj.Next(0, optionsList.Count)].FindElement(By.TagName("A"));
this.ElementClickEvent(optionLink);
}
if (titleType == "多选")
{
foreach (var optionItem in optionsList)
{
IWebElement optionLink = optionItem.FindElement(By.TagName("A"));
this.ElementClickEvent(optionLink);
}
}
}
}
//日日学-填入答案(填空)
private void WriteTitleAnswerTKDay(string answerText)
{
List<IWebElement> inputList = DriverObj.FindElements(By.ClassName("input_txt")).ToList();
if (answerText.Length > 0)
{
List<string> answerList = answerText.Split(',').ToList();
for (int iLoop = 0; iLoop < inputList.Count; iLoop++)
{
inputList[iLoop].SendKeys(answerList[iLoop]);
Thread.Sleep(1000);
}
}
else
{
for (int iLoop = 0; iLoop < inputList.Count; iLoop++)
{
inputList[iLoop].SendKeys("-");
Thread.Sleep(1000);
}
}
}
//日日学-填入答案(简答,案例)
private void WriteTitleAnswerWDDay(string answerText)
{
List<IWebElement> inputList = DriverObj.FindElements(By.ClassName("answer_area")).ToList();
for (int iLoop = 0; iLoop < inputList.Count; iLoop++)
{
inputList[iLoop].SendKeys(answerText);
Thread.Sleep(1000);
}
}
//日日学-打开下题
private void GotoNextTitleDay(string titleTypeTx)
{
List<IWebElement> nextButtonList = DriverObj.FindElements(By.CssSelector("[class='btn04_cui ml20']")).ToList();
if (nextButtonList.Count > 0)
{
Thread.Sleep(1000);
this.ElementClickEvent(nextButtonList[0]);
int sleepTime = this.ReturnSleepSecond(titleTypeTx);
this.OutAnswerLog(string.Format("第 {0} 题等待:{1} 秒", (this.CountNumDay + 1).ToString().PadLeft(3, ' '), sleepTime));
Thread.Sleep(1000 * sleepTime);
}
}
//日日学-获取题目
private string ReturnTitleContentDay()
{
string contentText = string.Empty;
List<IWebElement> titleElementList = DriverObj.FindElements(By.CssSelector(".subject_he h1")).ToList();
if (titleElementList.Count > 0)
{
contentText = "[题目]" + titleElementList[0].Text.Trim().Replace("\r\n", string.Empty);
}
return contentText;
}
//日日学-获取选项
private List<string> ReturnTitleOptionDay()
{
List<string> optionTextList = new List<string>();
List<IWebElement> contextGroupElementList = DriverObj.FindElements(By.ClassName("option_fl")).ToList();
if (contextGroupElementList.Count > 0)
{
List<IWebElement> contextItemElementList = contextGroupElementList[0].FindElements(By.TagName("LI")).ToList();
foreach (var contextItemElement in contextItemElementList)
{
string aswerItemElementText = contextItemElement.Text.Trim().Replace("\r\n", string.Empty);
optionTextList.Add("[选项]" + aswerItemElementText);
}
}
return optionTextList;
}
支持所有主流的浏览器
测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上。
测试系统功能——创建衰退测试检验软件功能和用户需求。
支持自动录制动作和自动生成DotNet, Java, Perl, Python等不同语言的测试脚本。
边栏推荐
猜你喜欢
随机推荐
MySQL字符串函数
SQL调优小记
JS garbage collection mechanism and memory leakage
SQL tuning notes
Case: check the empty field [annotation + reflection + custom exception]
yum install xxx报错
服务器端渲染(SSR)和客户端渲染(CSR)的区别
JVM garbage collector part 1
À propos de l'utilisation intelligente du flux et de la carte
Activiti目录(五)驳回、重新发起、取消流程
Activiti目录(一)重点介绍
Activiti directory (III) deployment process and initiation process
8086 memory
汇编课后作业
1. JVM入门介绍
Flink 解析(一):基础概念解析
【逆向中级】跃跃欲试
Take you hand-in-hand to do intensive learning experiments -- knock the level in detail
PostgreSQL 14.2, 13.6, 12.10, 11.15 and 10.20 releases
【MMdetection】一文解决安装问题