_csharp
TL;DR - Show Me The Code
using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace csharp
{
[TestClass]
public class FileUploadTest
{
IWebDriver _driver;
[TestInitialize]
public void SetUp()
{
_driver = new FirefoxDriver();
}
[TestCleanup]
public void TearDown()
{
_driver?.Quit();
}
[TestMethod]
public void UploadFileFromDisk()
{
string File = "SomeFile.txt";
_driver.Url = "https://the-internet.herokuapp.com/upload";
string baseDirectory = AppContext.BaseDirectory;
string relativePath = "../../../SomeFile.txt";
string uploadFile = Path.GetFullPath(Path.Combine(baseDirectory, relativePath));
_driver.FindElement(By.Id("file-upload")).SendKeys(uploadFile);
_driver.FindElement(By.Id("file-submit")).Click();
IWebElement fileName = _driver.FindElement(By.Id("uploaded-files"));
Assert.AreEqual(File, fileName.Text);
}
}
}
Code Walkthrough
Importing Libraries
Lines 1 to 5 are pulling in our requisite classes for interacting with the operating system (e.g., using System.IO;
),
our testing framework (e.g., using Microsoft.VisualStudio.TestTools.UnitTesting;
), driving the browser with
Selenium (e.g., using OpenQA.Selenium;
), and launching an instance of Firefox
(e.g., using OpenQA.Selenium.Firefox;
).
Setup and Teardown
After specifying the namespace and the class (e.g., public class FileUploadTest
), lines 12 to 24 are setting up and
tearing down the browser instance.
First we create a field variable (e.g., IWebDriver _driver;
) to store our WebDriver instance for reuse throughout
the class. We then create a SetUp()
method with a [TestInitialize]
attribute, so it runs before our test. In this
method we are creating an instance of WebDriver with Firefox (e.g., _driver = new FirefoxDriver();
).
After our test executes, the TearDown()
method will run thanks to the [TestCleanup]
attribute. This
calls _driver?.Quit();
which will close the browser instance.
The Test
Lines 29 to 37 are the test itself.
We create an UploadFileFromDisk()
method and add a [TestMethod]
attribute, so it is run as a test. In it, we leverage
a file from local disk by specifying the file (e.g., SomeFile.txt
) and its path (e.g., the file is assumed to be in
the same directory as the test).
Next we visit the page with the upload form, input the string value of uploadFile
(e.g., the full path to the file
plus the filename with its extension), and submit the form. After the file is uploaded to the page it will display the
filename it just processed. We use this text to perform our assertion (making sure the uploaded file is what we expect).
Executing the Test
Before executing the test, we need to make sure the required dependencies are declared on the project file (e.g.,
csharp.csproj
in this case).
Toggle to see the csharp.csproj
file.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.5.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.5.2" />
<PackageReference Include="Selenium.Support" Version="4.24.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.24.0" />
</ItemGroup>
</Project>
Finally, we can run the test by executing dotnet test
from the command-line.