Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dotnet] Replace WebRequest with HttpClient in tests #13471

Merged
merged 4 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions dotnet/test/common/ClickScrollingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void ShouldNotScrollIfAlreadyScrolledAndElementIsInView()
{
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("scroll3.html");
driver.FindElement(By.Id("button2")).Click();
long scrollTop = GetScrollTop();
double scrollTop = GetScrollTop();
driver.FindElement(By.Id("button1")).Click();
Assert.AreEqual(scrollTop, GetScrollTop());
}
Expand Down Expand Up @@ -217,7 +217,7 @@ public void ShouldBeAbleToClickElementThatIsOutOfViewInANestedFrameThatIsOutOfVi
public void ShouldNotScrollWhenGettingElementSize()
{
driver.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("scroll3.html");
long scrollTop = GetScrollTop();
double scrollTop = GetScrollTop();
Size ignoredSize = driver.FindElement(By.Id("button1")).Size;
Assert.AreEqual(scrollTop, GetScrollTop());
}
Expand Down Expand Up @@ -250,9 +250,9 @@ public void ShouldBeAbleToClickInlineTextElementWithChildElementAfterScrolling()
Assert.IsFalse(checkbox.Selected, "Checkbox should not be selected after click");
}

private long GetScrollTop()
private double GetScrollTop()
{
return (long)((IJavaScriptExecutor)driver).ExecuteScript("return document.body.scrollTop;");
return double.Parse(((IJavaScriptExecutor)driver).ExecuteScript("return document.body.scrollTop;").ToString());
}

private Func<bool> TitleToBe(string desiredTitle)
Expand Down
21 changes: 13 additions & 8 deletions dotnet/test/common/Environment/RemoteSeleniumServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http;

namespace OpenQA.Selenium.Environment
{
Expand Down Expand Up @@ -39,19 +40,22 @@ public void Start()
webserverProcess.Start();
DateTime timeout = DateTime.Now.Add(TimeSpan.FromSeconds(30));
bool isRunning = false;

// Poll until the webserver is correctly serving pages.
using var httpClient = new HttpClient();

while (!isRunning && DateTime.Now < timeout)
{
// Poll until the webserver is correctly serving pages.
HttpWebRequest request = WebRequest.Create("http://localhost:6000/wd/hub/status") as HttpWebRequest;
try
{
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using var response = httpClient.GetAsync("http://localhost:6000/wd/hub/status").GetAwaiter().GetResult();

if (response.StatusCode == HttpStatusCode.OK)
{
isRunning = true;
}
}
catch (WebException)
catch (Exception ex) when (ex is HttpRequestException || ex is TimeoutException)
{
}
}
Expand All @@ -65,14 +69,15 @@ public void Start()

public void Stop()
{
if (autoStart && (webserverProcess != null && !webserverProcess.HasExited))
if (autoStart && webserverProcess != null && !webserverProcess.HasExited)
{
HttpWebRequest request = WebRequest.Create("http://localhost:6000/selenium-server/driver?cmd=shutDownSeleniumServer") as HttpWebRequest;
using var httpClient = new HttpClient();

try
{
request.GetResponse();
using var response = httpClient.GetAsync("http://localhost:6000/selenium-server/driver?cmd=shutDownSeleniumServer").GetAwaiter().GetResult();
}
catch (WebException)
catch (Exception ex) when (ex is HttpRequestException || ex is TimeoutException)
{
}

Expand Down
21 changes: 12 additions & 9 deletions dotnet/test/common/Environment/TestWebServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,22 @@ public void Start()
TimeSpan timeout = TimeSpan.FromSeconds(30);
DateTime endTime = DateTime.Now.Add(TimeSpan.FromSeconds(30));
bool isRunning = false;

// Poll until the webserver is correctly serving pages.
using var httpClient = new HttpClient();

while (!isRunning && DateTime.Now < endTime)
{
// Poll until the webserver is correctly serving pages.
HttpWebRequest request = WebRequest.Create(EnvironmentManager.Instance.UrlBuilder.LocalWhereIs("simpleTest.html")) as HttpWebRequest;
try
{
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
using var response = httpClient.GetAsync(EnvironmentManager.Instance.UrlBuilder.LocalWhereIs("simpleTest.html")).GetAwaiter().GetResult();

if (response.StatusCode == HttpStatusCode.OK)
{
isRunning = true;
}
}
catch (WebException)
catch (Exception ex) when (ex is HttpRequestException || ex is TimeoutException)
{
}
}
Expand All @@ -154,16 +157,16 @@ public void Start()

public void Stop()
{
using (var httpClient = new HttpClient())
if (webserverProcess != null)
{
using (var quitResponse = httpClient.GetAsync(EnvironmentManager.Instance.UrlBuilder.LocalWhereIs("quitquitquit")).GetAwaiter().GetResult())
using (var httpClient = new HttpClient())
{
using (var quitResponse = httpClient.GetAsync(EnvironmentManager.Instance.UrlBuilder.LocalWhereIs("quitquitquit")).GetAwaiter().GetResult())
{

}
}
}

if (webserverProcess != null)
{
try
{
webserverProcess.WaitForExit(10000);
Expand Down
34 changes: 15 additions & 19 deletions dotnet/test/common/Environment/UrlBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Text;
using System.IO;
using System.Net.Http;

namespace OpenQA.Selenium.Environment
{
Expand Down Expand Up @@ -105,25 +105,21 @@ public string WhereIsSecure(string page)
public string CreateInlinePage(InlinePage page)
{
Uri createPageUri = new Uri(new Uri(WhereIs(string.Empty)), "createPage");
Dictionary<string, object> payloadDictionary = new Dictionary<string, object>();
payloadDictionary["content"] = page.ToString();

Dictionary<string, object> payloadDictionary = new Dictionary<string, object>
{
["content"] = page.ToString()
};

string commandPayload = JsonConvert.SerializeObject(payloadDictionary);
byte[] data = Encoding.UTF8.GetBytes(commandPayload);

HttpWebRequest request = HttpWebRequest.Create(createPageUri) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/json;charset=utf8";
request.ServicePoint.Expect100Continue = false;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();

HttpWebResponse response = request.GetResponse() as HttpWebResponse;
// StreamReader.Close also closes the underlying stream.
Stream responseStream = response.GetResponseStream();
StreamReader responseStreamReader = new StreamReader(responseStream, Encoding.UTF8);
string responseString = responseStreamReader.ReadToEnd();
responseStreamReader.Close();

using var httpClient = new HttpClient();

var postHttpContent = new StringContent(commandPayload, Encoding.UTF8, "application/json");

using var response = httpClient.PostAsync(createPageUri, postHttpContent).GetAwaiter().GetResult();

var responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();

// The response string from the Java remote server has trailing null
// characters. This is due to the fix for issue 288.
Expand Down
Loading