using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace HelloSelenium
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = null;
try
{
//1. 打开Chrome浏览器
driver = new ChromeDriver();
//2. 进入https://www.baidu.com/主页
driver.Navigate().GoToUrl("https://www.baidu.com/");
//3. 搜索框输入“Selenium”
driver.FindElement(By.Id("kw")).SendKeys("Selenium");
//4. 点击“百度一下”
driver.FindElement(By.Id("su")).Click();
}
finally
{
//5. 关闭Chrome浏览器
if (driver != null)
{
driver.Dispose();
}
}
}
}
}
curl -d @JsonFile2.json http://localhost:9515/session/36d903cbd2177c278b5d39bbe74a3318/url
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace HelloSelenium
{
class Program
{
static IList<HttpCmd> cmdArr;
static readonly string baseUrl = "http://localhost:9515/";
static readonly string extendUrlFormat1 = "session";
static readonly string extendUrlFormat2 = "session/{sessionId}/url";
static readonly string extendUrlFormat3 = "session/{sessionId}/element";
static readonly string extendUrlFormat4 = "session/{sessionId}/element/{elementId}/value";
static readonly string extendUrlFormat5 = "session/{sessionId}/element";
static readonly string extendUrlFormat6 = "session/{sessionId}/element/{elementId}/click";
static readonly string extendUrlFormat7 = "session/{sessionId}";
static readonly string extendUrlFormat8 = "shutdown";
static readonly string jsonData1 = @"{""desiredCapabilities"": { ""caps"": {""nativeEvents"": false, ""browserName"": ""chrome"", ""version"": """",""platform"": ""ANY""}}}";
static readonly string jsonData2 = @"{""url"":""https://www.baidu.com/""}";
static readonly string jsonData3 = @"{""using"":""css selector"",""value"":""#kw""}";
static readonly string jsonData4 = @"{""value"":[""Selenium""]}";
static readonly string jsonData5 = @"{""using"":""css selector"",""value"":""#su""}";
static readonly string jsonData6 = @"{}";
static Dictionary<string, string> dicSe = new Dictionary<string, string>()
{{"{sessionId}", null}};
static Dictionary<string, string> dicSeEl = new Dictionary<string, string>()
{{"{sessionId}", null},{"{elementId}", null}};
static Program()
{
cmdArr = new List<HttpCmd>()
{
new HttpCmd(null, null, null, null),
new HttpCmd("POST", extendUrlFormat1, jsonData1, null),
new HttpCmd("POST", extendUrlFormat2, jsonData2, dicSe),
new HttpCmd("POST", extendUrlFormat3, jsonData3, dicSe),
new HttpCmd("POST", extendUrlFormat4, jsonData4, dicSeEl),
new HttpCmd("POST", extendUrlFormat5, jsonData5, dicSe),
new HttpCmd("POST", extendUrlFormat6, jsonData6, dicSeEl),
new HttpCmd("DELETE", extendUrlFormat7, null, dicSe),
new HttpCmd("GET", extendUrlFormat8, null, null),
};
}
static void Main(string[] args)
{
Process p = null;
try
{
string response = null, sessionId = null, elementId = null, extendUrl;
JObject jObj = null;
//1. 打开Chrome浏览器
//启动chromedriver.exe
p = Process.Start(@"D:\Software\chromedriver.exe", "--port=9515");
Thread.Sleep(1000);
//启动chrome
response = HttpOp(cmdArr[1]);
jObj = JsonConvert.DeserializeObject(response) as JObject;
sessionId = jObj["sessionId"].Value<string>();
dicSe["{sessionId}"] = sessionId;
dicSeEl["{sessionId}"] = sessionId;
Thread.Sleep(1000);
//2. 进入https://www.baidu.com/主页
HttpOp(cmdArr[2]);
Thread.Sleep(1000);
//3. 搜索框输入“Selenium”
//获取elementId
response = HttpOp(cmdArr[3]);
jObj = JsonConvert.DeserializeObject(response) as JObject;
elementId = jObj["value"]["ELEMENT"].Value<string>();
dicSeEl["{elementId}"] = elementId;
//输入“Selenium”
HttpOp(cmdArr[4]);
Thread.Sleep(1000);
//4. 点击“百度一下”
//获取elementId
response = HttpOp(cmdArr[5]);
jObj = JsonConvert.DeserializeObject(response) as JObject;
elementId = jObj["value"]["ELEMENT"].Value<string>();
dicSeEl["{elementId}"] = elementId;
//点击
HttpOp(cmdArr[6]);
Thread.Sleep(1000);
//5. 关闭Chrome浏览器
//关闭Chrome
HttpOp(cmdArr[7]);
//关闭chromedriver
HttpOp(cmdArr[8]);
}
finally
{
if (p != null)
{
p.WaitForExit(3000);
p.Dispose();
}
}
}
private static string HttpOp(HttpCmd cmd)
{
var fullUrl = baseUrl + cmd.ExtendUrl;
HttpClient client = new HttpClient();
Task<HttpResponseMessage> response = null;
switch (cmd.Method)
{
case "GET":
response = client.GetAsync(fullUrl);
break;
case "DELETE":
response = client.DeleteAsync(fullUrl);
break;
case "POST":
HttpContent content = new StringContent(cmd.JsonData, Encoding.UTF8, "application/json");
response = client.PostAsync(fullUrl, content);
break;
}
return response.Result.Content.ReadAsStringAsync().Result;
}
internal class HttpCmd
{
public string Method { get; set; }
public string ExtendUrlFormat { get; set; }
public string JsonData { get; set; }
public Dictionary<string, string> ParaDictionary { get; set; }
public string ExtendUrl => BuildExtendUrl();
public HttpCmd(string method, string extendUrlFormat, string jsonData, Dictionary<string, string> paraDictionary)
{
this.Method = method;
this.ExtendUrlFormat = extendUrlFormat;
this.JsonData = jsonData;
this.ParaDictionary = paraDictionary;
}
private string BuildExtendUrl()
{
var extendUrl = ExtendUrlFormat;
if (ParaDictionary != null && ParaDictionary.Count > 0)
{
foreach (var pair in ParaDictionary)
{
extendUrl = extendUrl.Replace(pair.Key, pair.Value);
}
}
return extendUrl;
}
}
}
}
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |