当前位置:网站首页>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 .
边栏推荐
- MySQL basic addition, deletion, modification and query of SQL statements
- JVM garbage collector part 1
- Set up the flutter environment pit collection
- Redis quick start
- Activiti directory (III) deployment process and initiation process
- Models used in data warehouse modeling and layered introduction
- Use of mongodb in node
- Redis快速入门
- 【逆向】脱壳后修复IAT并关闭ASLR
- Control transfer instruction
猜你喜欢
Learn the wisdom of investment Masters
EasyRE WriteUp
MySQL date function
Akamai talking about risk control principles and Solutions
Serial serialold parnew of JVM garbage collector
C#版Selenium操作Chrome全屏模式显示(F11)
EasyRE WriteUp
Assembly language segment definition
Program counter of JVM runtime data area
Programmer orientation problem solving methodology
随机推荐
MySQL advanced (index, view, stored procedure, function, password modification)
Brush questions during summer vacation, ouch ouch
Some feelings of brushing leetcode 300+ questions
Akamai anti confusion
C# WinForm系列-Button简单使用
07个人研发的产品及推广-人力资源信息管理系统
Program counter of JVM runtime data area
Re signal writeup
程序员定位解决问题方法论
Flink源码解读(三):ExecutionGraph源码解读
学习投资大师的智慧
Detailed explanation of data types of MySQL columns
[reverse primary] Unique
Only learning C can live up to expectations TOP4 S1E6: data type
[ciscn 2021 South China]rsa writeup
Activit fragmented deadly pit
1. Introduction to JVM
Flink 解析(四):恢复机制
应用服务配置器(定时,数据库备份,文件备份,异地备份)
信息与网络安全期末复习(完整版)