-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
149 lines (114 loc) · 4.24 KB
/
Copy pathProgram.cs
File metadata and controls
149 lines (114 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Json;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
await new PcloudySeleniumRunner().RunAsync();
public class PcloudySeleniumRunner
{
private readonly string _hubUrl = "https://browser.device.pcloudy.com/seleniumcloud/wd/hub";
public async Task RunAsync()
{
RemoteWebDriver? driver = null;
try
{
var pcloudyOptions = new Dictionary<string, object>
{
["userName"] = "Your_Username",
["accessKey"] = "Your_Access_Key",
["os"] = "Windows",
["osVersion"] = "11",
["browserVersion"] = "144",
["seleniumVersion"] = "4.18.1",
["project"] = "Project_1",
["build"] = "Production Run",
["name"] = "Wikipedia Test",
["tag"] = "Wikipedia",
["local"] = false,
["pCloudy_EnableVideo"] = true,
["idleTimeout"] = 10
};
Console.WriteLine("========== pCloudy Options ==========");
Console.WriteLine(JsonSerializer.Serialize(
pcloudyOptions,
new JsonSerializerOptions { WriteIndented = true }));
var options = new ChromeOptions();
options.PlatformName = "Windows";
// IMPORTANT:
// Do NOT set options.BrowserVersion here.
// pCloudy will use browserVersion from pcloudy:options.
options.AddAdditionalOption("pcloudy:options", pcloudyOptions);
Console.WriteLine();
Console.WriteLine("Connecting to pCloudy Selenium Grid...");
Console.WriteLine(_hubUrl);
var watch = Stopwatch.StartNew();
driver = new RemoteWebDriver(
new Uri(_hubUrl),
options.ToCapabilities(),
TimeSpan.FromMinutes(5));
watch.Stop();
Console.WriteLine();
Console.WriteLine("======================================");
Console.WriteLine("Session Created Successfully");
Console.WriteLine($"Time Taken : {watch.Elapsed}");
Console.WriteLine($"Session ID : {driver.SessionId}");
Console.WriteLine("======================================");
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(120);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
driver.Manage().Window.Maximize();
Console.WriteLine("Opening Wikipedia...");
driver.Navigate().GoToUrl("https://www.wikipedia.org/");
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
// Step 1: Verify page title
Console.WriteLine($"Page Title: {driver.Title}");
// Step 2: Search for Selenium
var searchBox = wait.Until(d => d.FindElement(By.Name("search")));
searchBox.Clear();
searchBox.SendKeys("Selenium");
searchBox.SendKeys(Keys.Enter);
// Step 9: Capture screenshot
CaptureScreenshot(driver);
// Step 10: Print success message
Console.WriteLine("Wikipedia Selenium test completed successfully.");
await Task.CompletedTask;
CaptureScreenshot(driver);
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine("============= ERROR =============");
Console.WriteLine(ex.ToString());
Console.WriteLine("=================================");
throw;
}
finally
{
if (driver != null)
{
Console.WriteLine("Closing browser...");
driver.Quit();
}
}
}
private static void CaptureScreenshot(RemoteWebDriver driver)
{
try
{
var folder = Path.Combine(AppContext.BaseDirectory, "Screenshots");
Directory.CreateDirectory(folder);
var fileName = $"Screenshot_{DateTime.Now:yyyyMMdd_HHmmss}.png";
var path = Path.Combine(folder, fileName);
var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
screenshot.SaveAsFile(path);
Console.WriteLine($"Screenshot saved: {path}");
}
catch (Exception ex)
{
Console.WriteLine($"Unable to save screenshot: {ex.Message}");
}
}
}