1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . IO ;
4+ using System . Threading . Tasks ;
5+ using Microsoft . IdentityModel . Tokens ;
16using Microsoft . VisualStudio . TestTools . UnitTesting ;
7+ using OpenQA . Selenium ;
8+ using OpenQA . Selenium . Chrome ;
9+ using OpenQA . Selenium . Firefox ;
10+ using OpenQA . Selenium . Remote ;
11+ using OpenQA . Selenium . Support . UI ;
212
313namespace SeleniumDocs . Drivers
414{
515 [ TestClass ]
616 public class RemoteWebDriverTest : BaseTest
717 {
18+ [ TestInitialize ]
19+ public async Task Setup ( )
20+ {
21+ await StartServer ( ) ;
22+ }
23+
24+ [ TestMethod ]
25+ public void RunRemote ( )
26+ {
27+ var options = new ChromeOptions ( ) ;
28+ driver = new RemoteWebDriver ( GridUrl , options ) ;
29+
30+ Assert . IsInstanceOfType ( driver , typeof ( IHasDownloads ) ) ;
31+ }
32+
33+ [ TestMethod ]
34+ public void Uploads ( )
35+ {
36+ var options = new ChromeOptions ( ) ;
37+ driver = new RemoteWebDriver ( GridUrl , options ) ;
38+
39+ driver . Url = "https://the-internet.herokuapp.com/upload" ;
40+
41+ string baseDirectory = AppContext . BaseDirectory ;
42+ string relativePath = "../../../TestSupport/selenium-snapshot.png" ;
43+
44+ string uploadFile = Path . GetFullPath ( Path . Combine ( baseDirectory , relativePath ) ) ;
45+
46+ ( ( RemoteWebDriver ) driver ) . FileDetector = new LocalFileDetector ( ) ;
47+ IWebElement fileInput = driver . FindElement ( By . CssSelector ( "input[type=file]" ) ) ;
48+ fileInput . SendKeys ( uploadFile ) ;
49+ driver . FindElement ( By . Id ( "file-submit" ) ) . Click ( ) ;
50+
51+ IWebElement fileName = driver . FindElement ( By . Id ( "uploaded-files" ) ) ;
52+ Assert . AreEqual ( "selenium-snapshot.png" , fileName . Text ) ;
53+ }
54+
55+ [ TestMethod ]
56+ public void Downloads ( )
57+ {
58+ ChromeOptions options = new ChromeOptions
59+ {
60+ EnableDownloads = true
61+ } ;
62+ driver = new RemoteWebDriver ( GridUrl , options ) ;
63+
64+ driver . Url = "https://selenium.dev/selenium/web/downloads/download.html" ;
65+ driver . FindElement ( By . Id ( "file-1" ) ) . Click ( ) ;
66+ driver . FindElement ( By . Id ( "file-2" ) ) . Click ( ) ;
67+ WebDriverWait wait = new WebDriverWait ( driver , TimeSpan . FromSeconds ( 3 ) ) ;
68+ wait . Until ( d => ( ( RemoteWebDriver ) d ) . GetDownloadableFiles ( ) . Contains ( "file_2.jpg" ) ) ;
69+
70+ List < string > names = ( ( RemoteWebDriver ) driver ) . GetDownloadableFiles ( ) ;
71+
72+ Assert . IsTrue ( names . Contains ( "file_1.txt" ) ) ;
73+ Assert . IsTrue ( names . Contains ( "file_2.jpg" ) ) ;
74+ string downloadableFile = names [ 0 ] ;
75+ string targetDirectory = Path . Combine ( Path . GetTempPath ( ) , Guid . NewGuid ( ) . ToString ( ) ) ;
76+
77+ ( ( RemoteWebDriver ) driver ) . DownloadFile ( downloadableFile , targetDirectory ) ;
78+
79+ string fileContent = File . ReadAllText ( Path . Combine ( targetDirectory , downloadableFile ) ) ;
80+ Assert . AreEqual ( "Hello, World!" , fileContent . Trim ( ) ) ;
81+
82+ ( ( RemoteWebDriver ) driver ) . DeleteDownloadableFiles ( ) ;
83+
84+ Assert . IsTrue ( ( ( RemoteWebDriver ) driver ) . GetDownloadableFiles ( ) . IsNullOrEmpty ( ) ) ;
85+ Directory . Delete ( targetDirectory , recursive : true ) ;
86+ }
87+
88+ [ TestMethod ]
89+ public void CustomExecutor ( )
90+ {
91+ driver = new RemoteWebDriver ( GridUrl , new FirefoxOptions ( ) ) ;
92+ driver . Navigate ( ) . GoToUrl ( "https://www.selenium.dev/" ) ;
93+
94+ var customCommandDriver = driver as ICustomDriverCommandExecutor ;
95+ customCommandDriver . RegisterCustomDriverCommands ( FirefoxDriver . CustomCommandDefinitions ) ;
96+
97+ var screenshotResponse = customCommandDriver
98+ . ExecuteCustomDriverCommand ( FirefoxDriver . GetFullPageScreenshotCommand , null ) ;
99+
100+ Screenshot image = new Screenshot ( ( string ) screenshotResponse ) ;
101+
102+ string targetDirectory = Path . Combine ( Path . GetTempPath ( ) , Guid . NewGuid ( ) . ToString ( ) ) ;
103+ Directory . CreateDirectory ( targetDirectory ) ;
104+ string targetFile = Path . GetFullPath ( Path . Combine ( targetDirectory , "fullPage.png" ) ) ;
105+
106+ using ( var memoryStream = new MemoryStream ( image . AsByteArray ) )
107+ using ( var fileStream = new FileStream ( targetFile , FileMode . Create ) )
108+ {
109+ memoryStream . WriteTo ( fileStream ) ;
110+ }
111+
112+ Assert . IsTrue ( File . Exists ( targetFile ) ) ;
113+
114+ Directory . Delete ( targetDirectory , true ) ;
115+ }
8116 }
9117}
0 commit comments