当前位置:网站首页>Selenium test of automatic answer runs directly in the browser, just like real users.
Selenium test of automatic answer runs directly in the browser, just like real users.
2022-07-06 17:26:00 【Heart blue 168】
I received a small list a few days ago , In order to meet the hard requirements of the unit , How many questions must I learn online every day , How many questions must I practice online every day , So I want to find someone to develop an automatic question answering system . The picture above shows 《 Online learning of a military training contest 》 Demonstration of automatic answer .
With a curious heart , Find the implementation method of automatic answer software on the Internet , I wanted to use IFrame, Embed the answer page , Use jquery Operate on Web pages , But later I found that when I opened the answer window , Web page used target=_top, So this method doesn't work again , Only then Baidu strolls , Probably spent. 2 Hour or so , Automated test tool , namely :Selenium, You can operate directly through the browser .
This is also a discussion with colleagues about automatic system Patrol these days , Check the running state of the system , In the process , I found that my colleague wrote Demo It's also automatic , See things on the browser without mouse operation , Then you can move , It's fun .
"Selenium Test runs directly in browser , It's like a real user is doing it "
The official website is :http://www.selenium.org.cn/
Concrete C# The implementation process is as follows :
// Day school - Automatic answer
private void AutoAnswerTitleDay()
{
try
{
if (DriverObj.WindowHandles.Count <= 1)
{
this.OutAnswerLog(" Please make sure 【 Day school 】 The learning page of has been opened ...");
return;
}
DriverObj.SwitchTo().Window(DriverObj.WindowHandles[1]);
this.OutAnswerLog(string.Format(" The first {0} Question waiting :{1} second ", (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)
{
// Get question class
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;
}
// Jump out and learn
if (this.CountNumDay >= this.LearnCount)
{
break;
}
// Switch questions
this.MoveMouseToPoint(tabItem);
this.ElementClickEvent(tabItem);
Thread.Sleep(1000);
while (true)
{
try
{
// Topic type
string titleType = this.ReturnTitleType(titleTypeTx + " topic ");
// Get the title
string titleText = this.ReturnTitleContentDay();
if (titleText.Length == 0)
{
this.RecordSystemLog(" Unable to get the title ");
break;
}
// Jump out and learn
if (this.CountNumDay >= this.LearnCount)
{
this.RecordSystemLog(" The number of studies has been reached ");
break;
}
// Get options
List<string> optionTextList = this.ReturnTitleOptionDay();
List<string> answerItemList = this.FindTitleAnswer(titleType, titleTypeTx, titleText);
// Fill in the answer
if (" The radio | multi-select | Judge ".IndexOf(titleTypeTx) > -1)
{
this.SelectTitleAnswerDay(answerItemList, titleTypeTx);
}
if (" Fill in the blanks ".IndexOf(titleTypeTx) > -1)
{
this.WriteTitleAnswerTKDay(answerItemList[0]);
}
if (" Short answer | Case study ".IndexOf(titleTypeTx) > -1)
{
this.WriteTitleAnswerWDDay(answerItemList[0]);
}
// Open the next question
this.CountNumDay++;
this.GotoNextTitleDay(titleTypeTx);
}
catch (Exception xe)
{
this.RecordSystemLog(" There was an error in answering the questions of daily school -B:" + xe.Message);
}
}
}
}
catch (Exception xe)
{
this.RecordSystemLog(" There was an error in answering the questions of daily school -A:" + xe.Message);
}
}
// Day school - Callback function
private void SelectTitleAnswerDayCallBack(IAsyncResult ar)
{
this.PanelTools.Enabled = true;
this.OutAnswerLog(" The answer is over , I studied together " + this.CountNumDay + " topic " + (this.CountNumDay < this.LearnCount ? ", Insufficient questions , Please study again ." : "."));
MainForm.FlashWindow(this.Handle, true);
}
// Day school - Choose the answer ( The radio , multi-select , Judge )
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)
{
// The radio | multi-select
if (answerItemList.Exists(c => c == optionItemName.Substring(optionLinkName.Length)) == true)
{
isSelectAnswer = true;
this.MoveMouseToPoint(optionLink);
this.ElementClickEvent(optionLink);
}
}
else
{
// Judge
if (answerItemList.Exists(c => c == optionItemName) == true)
{
isSelectAnswer = true;
this.MoveMouseToPoint(optionLink);
this.ElementClickEvent(optionLink);
}
}
}
if (isSelectAnswer == false)
{
Random randObj = new Random();
if (titleType == " The radio " || titleType == " Judge ")
{
IWebElement optionLink = optionsList[randObj.Next(0, optionsList.Count)].FindElement(By.TagName("A"));
this.ElementClickEvent(optionLink);
}
if (titleType == " multi-select ")
{
foreach (var optionItem in optionsList)
{
IWebElement optionLink = optionItem.FindElement(By.TagName("A"));
this.ElementClickEvent(optionLink);
}
}
}
}
// Day school - Fill in the answer ( Fill in the blanks )
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);
}
}
}
// Day school - Fill in the answer ( Short answer , Case study )
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);
}
}
// Day school - Open the next question
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(" The first {0} Question waiting :{1} second ", (this.CountNumDay + 1).ToString().PadLeft(3, ' '), sleepTime));
Thread.Sleep(1000 * sleepTime);
}
}
// Day school - Get the title
private string ReturnTitleContentDay()
{
string contentText = string.Empty;
List<IWebElement> titleElementList = DriverObj.FindElements(By.CssSelector(".subject_he h1")).ToList();
if (titleElementList.Count > 0)
{
contentText = "[ subject ]" + titleElementList[0].Text.Trim().Replace("\r\n", string.Empty);
}
return contentText;
}
// Day school - Get options
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("[ Options ]" + aswerItemElementText);
}
}
return optionTextList;
}
Support all major browsers
Test compatibility with browser —— Test your application to see if it works well on different browsers and operating systems .
Test system functions —— Create a decay test to verify software functionality and user requirements .
Support automatic recording of actions and automatic generation DotNet, Java, Perl, Python Test scripts in different languages .
边栏推荐
- Final review of information and network security (based on the key points given by the teacher)
- Activiti directory (III) deployment process and initiation process
- 唯有学C不负众望 TOP2 p1变量
- Coursera cannot play video
- Akamai anti confusion
- EasyRE WriteUp
- 案例:检查空字段【注解+反射+自定义异常】
- JVM之垃圾回收器下篇
- Mongodb learning notes
- 唯有学C不负众望 TOP5 S1E8|S1E9:字符和字符串&&算术运算符
猜你喜欢
Activit fragmented deadly pit
Flink 解析(二):反压机制解析
手把手带你做强化学习实验--敲级详细
Final review of information and network security (full version)
Coursera cannot play video
[reverse] repair IAT and close ASLR after shelling
C# WinForm中DataGridView单元格显示图片
Log4j2 major vulnerabilities and Solutions
Flink源码解读(三):ExecutionGraph源码解读
复盘网鼎杯Re-Signal Writeup
随机推荐
Von Neumann architecture
唯有学C不负众望 TOP5 S1E8|S1E9:字符和字符串&&算术运算符
C#WinForm中的dataGridView滚动条定位
Redis quick start
06个人研发的产品及推广-代码统计工具
华为认证云计算HICA
Use of mongodb in node
MySQL Advanced (index, view, stored procedures, functions, Change password)
Control transfer instruction
Yum install XXX reports an error
Resume of a microservice architecture teacher with 10 years of work experience
JVM之垃圾回收器上篇
沉淀下来的数据库操作类-C#版(SQL Server)
Case: check the empty field [annotation + reflection + custom exception]
Flink源码解读(二):JobGraph源码解读
C# WinForm中DataGridView单元格显示图片
JVM class loading subsystem
案例:检查空字段【注解+反射+自定义异常】
JVM garbage collection overview
ByteDance overseas technical team won the championship again: HD video coding has won the first place in 17 items