Skip to content

Commit c38eccb

Browse files
quigamdevcafour
authored andcommitted
Added support for fileupload in DotVVM 3.1 apps
1 parent 68b66af commit c38eccb

File tree

7 files changed

+131
-20
lines changed

7 files changed

+131
-20
lines changed

README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ Framework is based on selenium wrappers and principles which canopy uses.
66

77
# APIs
88

9-
We created types of 3 APIs.
9+
We created types of 2 APIs.
1010
- AssertAPI
11-
- FluentAPI
12-
- LambdaAPI - *just concept - not implemented*
11+
- FluentAPI - discontinued due to lack of interest.
1312

1413
## Assert API
15-
Assert API uses concept of one assertion on one line. This approach has its own advantages when you need to quickly navigate between stack trace and source code.
14+
Assert API uses concept of one assertation on one line. This approach has big advantage when you need to quickly navigate between stack trace and source code.
1615

1716
```
1817
AssertUI.InnerTextEquals(element, "expected value", "Custom error message.");
1918
```
2019

21-
## Fluent API
20+
## Fluent API (discontinued)
2221
Fluent API is perfect for really simple tests. You can stack more checking methods in a row.
2322

2423
```

src/Integrations/Riganti.Selenium.DotVVM.MSTest2/DotVVMAssert.cs

+39-13
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,50 @@ public static void UploadFile(IElementWrapper element, string fullFileName)
1616
if (element.BrowserWrapper.IsDotvvmPage())
1717
{
1818
element.BrowserWrapper.LogVerbose("Selenium.DotVVM : Uploading file");
19-
var name = element.GetTagName();
20-
if (name == "a" && element.HasAttribute("onclick") && (element.GetAttribute("onclick")?.Contains("showUploadDialog") ?? false))
21-
{
22-
UploadFileByA(element, fullFileName);
23-
return;
24-
}
19+
var isLessThenDotvvm30 = element.BrowserWrapper.GetJavaScriptExecutor()
20+
.ExecuteScript("return !!(window[\"dotvvm\"] && dotvvm && dotvvm.fileUpload && dotvvm.fileUpload.createUploadId )|| false") as bool? ?? false;
2521

26-
if (name == "div" && element.FindElements("iframe", SelectBy.CssSelector).Count == 1)
22+
if (isLessThenDotvvm30)
2723
{
28-
UploadFileByDiv(element, fullFileName);
29-
return;
24+
element.BrowserWrapper.LogVerbose("Selenium.DotVVM : Uploading file");
25+
var name = element.GetTagName();
26+
if (name == "a" && element.HasAttribute("onclick") && (element.GetAttribute("onclick")?.Contains("showUploadDialog") ?? false))
27+
{
28+
UploadFileByA(element, fullFileName);
29+
return;
30+
}
31+
32+
if (name == "div" && element.FindElements("iframe", SelectBy.CssSelector).Count == 1)
33+
{
34+
UploadFileByDiv(element, fullFileName);
35+
return;
36+
}
37+
else
38+
{
39+
element.BrowserWrapper.LogVerbose("Selenium.DotVVM : Cannot identify DotVVM scenario. Uploading over standard procedure.");
40+
41+
element.BrowserWrapper.OpenInputFileDialog(element, fullFileName);
42+
return;
43+
}
3044
}
3145
else
3246
{
33-
element.BrowserWrapper.LogVerbose("Selenium.DotVVM : Cannot identify DotVVM scenario. Uploading over standard procedure.");
47+
var name = element.GetTagName();
48+
if (name == "a" && element.HasAttribute("onclick") && (element.GetAttribute("onclick")?.Contains("showUploadDialog") ?? false))
49+
{
50+
element = element.ParentElement.ParentElement;
51+
}
3452

35-
element.BrowserWrapper.OpenInputFileDialog(element, fullFileName);
36-
return;
53+
if (element.GetTagName() == "div")
54+
{
55+
var fileInput = element.Single("input[type=file]");
56+
fileInput.SendKeys(fullFileName);
57+
58+
element.Wait(element.ActionWaitTime);
59+
return;
60+
}
61+
62+
element.BrowserWrapper.LogVerbose("Selenium.DotVVM : Cannot identify DotVVM scenario. Uploading over standard procedure.");
3763
}
3864
}
3965

@@ -54,7 +80,7 @@ private static void UploadFileByDiv(IElementWrapper element, string fullFileName
5480

5581
element.Wait(element.ActionWaitTime);
5682
element.ActivateScope();
57-
83+
5884
}
5985

6086
private static void UploadFileByA(IElementWrapper element, string fullFileName)

src/Riganti.Selenium.DotVVM3Samples/Riganti.Selenium.DotVVM3.Samples.csproj

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
</PropertyGroup>
55
<ItemGroup>
66
<Content Include="**/*.dothtml;**/*.dotmaster;**/*.dotcontrol" Exclude="obj/**/*.*;bin/**/*.*" CopyToPublishDirectory="Always" />
7+
<Compile Remove="temp\**" />
8+
<Content Remove="temp\**" />
9+
<EmbeddedResource Remove="temp\**" />
710
<None Remove="**/*.dothtml;**/*.dotmaster;**/*.dotcontrol" />
11+
<None Remove="temp\**" />
812
</ItemGroup>
913
<ItemGroup>
1014
<None Remove="dotvvm_serialized_config.json.tmp" />
1115
</ItemGroup>
1216
<ItemGroup>
13-
<PackageReference Include="DotVVM.AspNetCore" Version="3.0.0" />
17+
<PackageReference Include="DotVVM.AspNetCore" Version="3.0.3" />
1418
</ItemGroup>
1519
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using DotVVM.Framework.Controls;
2+
using DotVVM.Framework.ViewModel;
3+
4+
namespace Riganti.Selenium.DotVVM3.Samples.ViewModels
5+
{
6+
public class FileUploadViewModel : DotvvmViewModelBase
7+
{
8+
public UploadedFilesCollection Files { get; set; } = new UploadedFilesCollection();
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@viewModel Riganti.Selenium.DotVVM3.Samples.ViewModels.FileUploadViewModel, Riganti.Selenium.DotVVM3.Samples
2+
<!DOCTYPE html>
3+
4+
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
5+
<head>
6+
<meta charset="utf-8" />
7+
<title></title>
8+
</head>
9+
<body>
10+
<dot:FileUpload ID="FUpload" UploadedFiles="{value: Files}" UploadButtonText="text" />
11+
</body>
12+
</html>

src/Tests/Riganti.Selenium.Core.Samples.Tests/UrlTests.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Riganti.Selenium.Core.Samples.FluentApi.Tests
1111
[TestClass]
1212
public class UrlTests : SeleniumTest
1313
{
14+
[TestMethod]
1415
public void Url_CheckHyperLink_Relative()
1516
{
1617
this.RunInAllBrowsers(browser =>
@@ -26,7 +27,8 @@ public void Url_CheckHyperLink_Relative()
2627
browser.CheckIfHyperLinkEquals("#RelativeLink", "path/test?query=test#fragmentasd", UrlKind.Relative,
2728
UriComponents.PathAndQuery);
2829
browser.CheckIfHyperLinkEquals("#RelativeLink", "path/test?query=test#fragment", UrlKind.Relative,
29-
UriComponents.PathAndQuery);
30+
UriComponents.PathAndQuery);
31+
browser.CheckIfHyperLinkEquals("#RelativeLink", "path/test", UrlKind.Relative,UriComponents.Path);
3032
});
3133
}
3234

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Riganti.Selenium.Core;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Riganti.Selenium.DotVVM;
8+
using System.IO;
9+
using Xunit;
10+
using Xunit.Abstractions;
11+
12+
namespace Riganti.Selenium.DotVVM3.Samples.Tests
13+
{
14+
public class FileUploadTests : AppSeleniumTest
15+
{
16+
public FileUploadTests(ITestOutputHelper output) : base(output)
17+
{
18+
}
19+
20+
[Fact]
21+
public void FileUpload_ElementWrapperByDiv()
22+
{
23+
RunInAllBrowsers(browser =>
24+
{
25+
browser.NavigateToUrl("/FileUpload");
26+
27+
var tempPath = Path.GetTempFileName();
28+
File.WriteAllBytes(tempPath, Enumerable.Range(0, 255).Select(i => (byte)i).ToArray());
29+
30+
var elm = browser.First("#FUpload", SelectBy.CssSelector);
31+
DotVVMAssert.UploadFile(browser.First("#FUpload"), tempPath);
32+
33+
browser.WaitFor(() =>
34+
{
35+
AssertUI.InnerTextEquals(browser.First("#FUpload .dotvvm-upload-files"), "1 files", false);
36+
}, 4000, "File upload failed.");
37+
});
38+
}
39+
[Fact]
40+
public void FileUpload_ElementWrapperByA()
41+
{
42+
RunInAllBrowsers(browser =>
43+
{
44+
browser.NavigateToUrl("/FileUpload");
45+
46+
var tempPath = Path.GetTempFileName();
47+
File.WriteAllBytes(tempPath, Enumerable.Range(0, 255).Select(i => (byte)i).ToArray());
48+
49+
DotVVMAssert.UploadFile(browser.First(".dotvvm-upload-button a"), tempPath);
50+
51+
browser.WaitFor(() =>
52+
{
53+
AssertUI.InnerTextEquals(browser.First("#FUpload .dotvvm-upload-files"), "1 files", false);
54+
}, 4000, "File upload failed.");
55+
});
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)