"Appnium+c#+winform Automation Test Series" One, get a native connected device, start multiple appnium, and get a native boot Appnium

Source: Internet
Author: User
Tags appium

This series of content, prepared according to the completion of the project as a baseline, step-by-step to the entire design and implementation process carding.

Start with some basic environmental problems, comb clear about mobile devices and appnium. Because we need the device name and the boot Appnium instance and the corresponding port number when we build the Appnium connection later.

First, access to mobile devices

To get the native connected device, we can use the ADB command through the console to implement, in the console input command ADB devices can have the following results:

Since you can

So simple to get to the connected device, the next is the console text processing analysis, and finally the need to get the content to show in the dropdown box.

For the obtained results, we take only the attached later, which can be implemented by the interception of strings. After removing the space and line breaks, it becomes a series of characters that are separated by device names so that only normal character processing can be done.

The actual code is as follows, for reference:

<summary>
Obtain the current machine connection terminal equipment
</summary>
<returns></returns>
public static string[] Initdevices ()
{
System.Diagnostics.Process p = new System.Diagnostics.Process ();
p.StartInfo.FileName = "cmd.exe";
P.startinfo.useshellexecute = false; Whether to start with the operating system shell
P.startinfo.redirectstandardinput = true;//accepts input from the calling program
P.startinfo.redirectstandardoutput = true;//gets output information from the calling program
P.startinfo.redirectstandarderror = true;//REDIRECT standard error output
P.startinfo.createnowindow = true;//does not display the program window
P.start ();//Startup program
Send input information to the CMD window
P.standardinput.writeline ("adb devices" + "&exit");
P.standardinput.writeline ("adb devices" + "&exit");
P.standardinput.autoflush = true;
P.standardinput.writeline ("Exit");
Writes the command to be executed to the standard input. The use of & here is a symbol of the batch command, indicating that the previous command executes the following (exit) command regardless of whether the execution succeeds, and if the exit command is not executed, the subsequent call to the ReadToEnd () method will feign death.
Similar symbols are also available in && | | The former indicates that the previous command must be successful before the subsequent command is executed, which means that the previous command execution must fail before the subsequent command is executed

Gets the output information of the CMD window
String output = P.standardoutput.readtoend ();
string[] Devices = output. Replace ("\ r \ n", ""). Split (new string[] {"Attached"}, Stringsplitoptions.removeemptyentries) [1]. Replace ("\ T", ""). Replace ("", ""). Split (new string[] {"Device"}, Stringsplitoptions.removeemptyentries);
p.WaitForExit ();//wait for the program to finish executing the exit process
P.close ();
return Devices;

}

Second, how to start multiple Appnium

According to the usual use method, directly double-click the Appnium program and click Start to launch a default port of 4723 Appnium instance. In this way, it seems that a machine can only run a appnium.

However, I found that when starting Appnium, print out a piece of code-like things:

Through this code, I found that the startup of Appnium seems to be based on the main program and some configuration to complete the instance boot. In this case, it should be possible to control the boot Appnium instance by modifying the IP, port, platform, and version. If I want to start multiple words,

It should be possible to run multiple appnium by configuring the port number. Sure enough, directly copy the command and the absolute path of the configuration file, execute in the console, and then observe the corresponding node program, really appeared! After a post-validation, this is done through the command instance session

The Appnium can be run.

In this case, the function that launches the Appnium instance is implemented by using the port as a parameter:

//<summary>
//Start Appnium instance
//</summary>
//<param name= "Port" > Start Appnium Port </ Param>
public static void Bootappnium (string Port)
{
System.Diagnostics.Process p = new System.Diagnostics . Process ();
p.StartInfo.FileName = "cmd.exe";
P.startinfo.useshellexecute = false;//whether to use operating system shell startup
P.startinfo.redirectstandardinput = true;// Accepts input from the caller
P.startinfo.redirectstandardoutput = true;//gets output information from the caller
P.startinfo.redirectstandarderror = true;//REDIRECT standard error output
P.startinfo.createnowindow = true;//do not show program window
P.start ();//Startup program

Send input information to the CMD window
string command = @ "D:\Appium\node.exe D:\Appium\node_modules\appium\lib\server\main.js--address 127.0.0.1--port" + Port + "--platform-name Android--platform-version--automation-name Appium--language zh_cn--log-no-color" + "&e XIT ";
P.standardinput.writeline (command);
P.standardinput.writeline ("Netstat-aon|findstr \" 127.0.0.1:47\ "" + "&exit");
P.standardinput.autoflush = true;
p.EnableRaisingEvents = true;

P.close ();

}

Third, get the appnium of native boot

Once you know how to start multiple appnium, it's easy to get a native boot appnium. We can obtain the PID value of the Node.exe program, and then according to the PID value to obtain the corresponding port value, of course, when we start the port, it is best to pre-

Set some range so that the port is not occupied.

First, get the PID value of the Node.exe:

c:\users\zeech>tasklist/v |findstr "Node"
Node.exe 6076 Console 9 78,948 K Unknown Zeech_lee\zeech 0:00:03 N/A

Where 6076 is the corresponding PID value, because the PID value is the current system is unique, so the PID value can be obtained to the Appnium start port number

C:\users\zeech>netstat-ano |findstr "6076"
TCP 127.0.0.1:4723 0.0.0.0:0 LISTENING 6076

The code is as follows:

public static string Getnodepid ()
{
System.Diagnostics.Process p = new System.Diagnostics.Process ();
p.StartInfo.FileName = "cmd.exe";
P.startinfo.useshellexecute = false; Whether to start with the operating system shell
P.startinfo.redirectstandardinput = true;//accepts input from the calling program
P.startinfo.redirectstandardoutput = true;//gets output information from the calling program
P.startinfo.redirectstandarderror = true;//REDIRECT standard error output
P.startinfo.createnowindow = true;//does not display the program window
P.start ();//Startup program

Send input information to the CMD window
P.standardinput.writeline ("tasklist/v |findstr \" node.exe\ "" + "&exit");
P.standardinput.writeline ("Netstat-aon|findstr \" 127.0.0.1:472\ "" + "&exit");
P.standardinput.autoflush = true;
P.standardinput.writeline ("Exit");
Writes the command to be executed to the standard input. The use of & here is a symbol of the batch command, indicating that the previous command executes the following (exit) command regardless of whether the execution succeeds, and if the exit command is not executed, the subsequent call to the ReadToEnd () method will feign death.
Similar symbols are also available in && | | The former indicates that the previous command must be successful before the subsequent command is executed, which means that the previous command execution must fail before the subsequent command is executed

Gets the output information of the CMD window
String output = P.standardoutput.readtoend ();
p.WaitForExit ();//wait for the program to finish executing the exit process
P.close ();
String nodepid = ((output. Replace ("\ r \ n", ""). Replace ("", ""). Split (new string[] {"Node.exe", "Console"}, Stringsplitoptions.removeemptyentries))) [2]. ToString ();

return nodepid;
}

//<summary>
//Get Appnium
//</summary> for the current machine,
//<returns></returns>
public static string [] Initappnium ()
{
System.Diagnostics.Process p = new System.Diagnostics.Process ();
P.st Artinfo.filename = "cmd.exe";
P.startinfo.useshellexecute = false;//whether to use operating system shell startup
P.startinfo.redirectstandardinput = true;// Accepts input from the caller
P.startinfo.redirectstandardoutput = true;//gets output information from the caller
P.startinfo.redirectstandarderror = true;//REDIRECT standard error output
P.startinfo.createnowindow = true;//do not show program window
P.start ();//Startup program

//Send input information to the CMD window
p.standardinput.writeline ("netstat-aon|findstr \" "+ getnodepid () +" \ "" + "&exit");
//P. Standardinput.writeline ("Netstat-aon|findstr \" 127.0.0.1:472\ "" + "&exit");
P.standardinput.autoflush = true;
//p.standardinput.writeline ("exit");
//writes to the standard input the command to execute. The use of & here is a symbol of the batch command, indicating that the previous command executes the following (exit) command regardless of whether the execution succeeds or not, and if you do not execute the exit command, the subsequent call to the ReadToEnd () method will feign death
//the same symbol also has && and | | The former indicates that the previous command must succeed before executing the command, which indicates that the previous command execution must fail before executing the command

//Get the output information of the CMD window
string outputs = P. Standardoutput.readtoend ();
p.WaitForExit ();//waits for the program to finish executing the exit process
P.close ();
string[] Appniuma = ((output. Replace ("\ r \ n", ""). Replace ("", ""). Replace ("0.0.0.0:0", "| | | 0.0.0.0:0 "). Split (new string[] {"TCP", "6076"}, Stringsplitoptions.removeemptyentries)));
list<string> appnium=new list<string> ();
for (int i = 0; i < Appniuma.count (); i++)
{
if (i>1)
{
Appnium.add (appniuma[i]);
}
}
return Appnium.toarray ();
}

Note: This type of string interception may cause some bugs. For example, in some cases, the PID value happens to be a part of a port number, which is also filtered out, the most perfect way is to only match the PID value, so processing can avoid this situation.

Of course, it is possible to agree in other processes that the screening is inaccurate, and that multiple debug analyses are required to be as precise as possible. Under Windows, you can download an awk plug-in that can be easily filtered for columns. When dealing with replacements, it is possible for readers to send

Now replace ("0.0.0.0:0", "| | 0.0.0.0:0 "), in fact, the purpose here is to separate the port number under the listener, to prevent confusion.

Iv. Destruction of Appnium instances

The process of destroying and creating is an inverse process, and with the above knowledge, it is very easy to deal with them. Only the Kill instance program Node.exe is required.

Directly on the code:

<summary>
Destroying Appnium instances
</summary>
public static void Exitappnium ()
{
System.Diagnostics.Process p = new System.Diagnostics.Process ();
p.StartInfo.FileName = "cmd.exe";
P.startinfo.useshellexecute = false; Whether to start with the operating system shell
P.startinfo.redirectstandardinput = true;//accepts input from the calling program
P.startinfo.redirectstandardoutput = true;//gets output information from the calling program
P.startinfo.redirectstandarderror = true;//REDIRECT standard error output
P.startinfo.createnowindow = true;//does not display the program window
P.start ();//Startup program

Send input information to the CMD window
P.standardinput.writeline (@ "taskkill/f/t/im node.exe" + "&exit");

P.standardinput.autoflush = true;
p.EnableRaisingEvents = true;

P.close ();

}

V. Summary

After combing the above, in the operation of the environment, need to use a lot of Windows commands. If you want to implement a complete Windows platform Automation test, you need to be familiar with the cmd command. At the same time to achieve a function, the most

The quick and easy way is to understand the principle. Anything in the understanding of the principle of the case, the processing is handy, the code is just a bridge, I believe that the above code written will feel very simple, can say

Almost all of the processing of some strings. In the process of testing, it is often necessary to analyze the obtained results, and how to obtain the information accurately will be the key to the success of the test.

Thank you, I can't wait to conceive the next blog post!

"Appnium+c#+winform Automation Test Series" One, get a native connected device, start multiple appnium, and get a native boot Appnium

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.