-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ability to click inside the browser
- Loading branch information
1 parent
30cbfca
commit bd3e1fb
Showing
6 changed files
with
109 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
public enum PageActionType | ||
{ | ||
ScrollToEnd, | ||
Wait | ||
Wait, | ||
Click | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
Tests/CocoCrawler.IntegrationTests/Scenarios/OpenLinkAndClick/OpenLinkAndClickTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using CocoCrawler.Builders; | ||
using CocoCrawler.Scheduler; | ||
using FluentAssertions; | ||
using WireMock.Server; | ||
|
||
namespace CocoCrawler.IntegrationTests.Scenarios.OpenLinkAndClick; | ||
|
||
[Collection(nameof(BrowserCollection))] | ||
public class OpenLinkAndClickTests | ||
{ | ||
private readonly WireMockServer _wireMockServer = WireMockServer.Start(); | ||
|
||
[Fact] | ||
public async Task DocumentShould_Click_WhenCalled() | ||
{ | ||
// Arrange | ||
_wireMockServer.ReturnSuccessWithPage($"{_wireMockServer.Url}/clickme", GeStartPage(_wireMockServer.Url!)); | ||
_wireMockServer.ReturnSuccessWithPage($"{_wireMockServer.Url}/next-page", GetSecondPage()); | ||
|
||
var crawlerEngine = await new CrawlerEngineBuilder() | ||
.AddPage($"{_wireMockServer.Url}/clickme", pageOptions => pageOptions | ||
.OpenLinks("div.content > a", subPageOptions => | ||
{ | ||
subPageOptions.ConfigurePageActions(actions => | ||
{ | ||
actions.Click("button#clickme"); | ||
}); | ||
subPageOptions.ExtractObject([new("Was i clicked", "div.clicked-now-scraped")]); | ||
}) | ||
.AddOutputToCsvFile("clicked-results.txt", cleanOnStartup: true) | ||
) | ||
.ConfigureEngine(options => options.WithScheduler(new InMemoryScheduler(totalSecondsTimeoutAfterJob: 2))) | ||
.BuildAsync(); | ||
|
||
// Act | ||
await crawlerEngine.RunAsync(); | ||
|
||
// Assert | ||
var fileOutputContents = File.ReadAllText("clicked-results.txt"); | ||
|
||
var expectedContents = $@"Url,Was i clicked | ||
{_wireMockServer.Url}/next-page,Yes i was! | ||
"; | ||
|
||
fileOutputContents.Should().Be(expectedContents); | ||
} | ||
|
||
private static string GeStartPage(string baseUrl) | ||
{ | ||
return $@" | ||
<html> | ||
<body> | ||
<div class=""content""> | ||
<a href='{baseUrl}/next-page'>Click me</a> | ||
</div> | ||
</body> | ||
</html>"; | ||
} | ||
|
||
private static string GetSecondPage() | ||
{ | ||
return @" | ||
<!DOCTYPE html> | ||
<html lang=""en"" xmlns=""http://www.w3.org/1999/xhtml""> | ||
<body> | ||
<button id=""clickme""> ClickMe </button> | ||
<script> | ||
document.getElementById('clickme').addEventListener('click', function() { | ||
var div = document.createElement('div'); | ||
div.className = 'clicked-now-scraped'; | ||
div.textContent = 'Yes i was!'; | ||
document.body.appendChild(div); | ||
}); | ||
</script> | ||
</body> | ||
</html>"; | ||
} | ||
} |