-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathGoogleSearchSteps.cs
108 lines (96 loc) · 3.53 KB
/
GoogleSearchSteps.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using TechTalk.SpecFlow;
namespace SpecFlowLambdaSample
{
[Binding]
public sealed class GoogleSearch
{
private IWebDriver _driver;
private IWebElement searchBox;
private LambdaTestDriver LTDriver = null;
String test_url = "https://www.google.com/";
public GoogleSearch(ScenarioContext ScenarioContext)
{
LTDriver = (LambdaTestDriver)ScenarioContext["LTDriver"];
}
[Given(@"that I am on the Google app (.*) and (.*)")]
public void GivenThatIAmOnTheBingAppAnd(string profile, string environment)
{
_driver = LTDriver.Init(profile, environment);
_driver.Url = test_url;
_driver.Manage().Window.Maximize();
System.Threading.Thread.Sleep(2000);
}
[Given(@"that I open the Google app with (.*), (.*), (.*), (.*), and (.*)")]
public void GivenThatIOpenTheBingAppWithAnd(
string build,
string name,
string platform,
string browserName,
string version
)
{
_driver = LTDriver.InitLocal(build, name, platform, browserName, version);
_driver.Url = test_url;
_driver.Manage().Window.Maximize();
System.Threading.Thread.Sleep(2000);
}
[Then(@"click on the text box")]
public void ThenClickOnTheTextBox()
{
searchBox = _driver.FindElement(By.XPath("//*[@id='APjFqb']"));
searchBox.Click();
System.Threading.Thread.Sleep(4000);
}
[Then(@"search for LambdaTest")]
public void ThenSearchForLambdaTest()
{
searchBox.SendKeys("lambdatest.com" + Keys.Enter);
System.Threading.Thread.Sleep(2000);
}
[Then(@"click on the first result")]
public void ThenClickOnTheFirstResult()
{
System.Threading.Thread.Sleep(5000);
int maxScrollAttempts = 5;
bool elementFound = false;
for (int i = 0; i < maxScrollAttempts && !elementFound; i++)
{
try
{
IWebElement secondCheckBox = _driver.FindElement(
By.XPath(
"//*[@id='rso']/div[1]/div/div/div/div/div/div/div/div[1]/div/span/a/h3"
)
);
secondCheckBox.Click();
elementFound = true;
}
catch (NoSuchElementException)
{
// Scroll the page
IJavaScriptExecutor js = (IJavaScriptExecutor)_driver;
js.ExecuteScript("window.scrollTo(0, document.body.scrollHeight);");
System.Threading.Thread.Sleep(2000); // Wait for the scroll to complete
}
}
if (!elementFound)
{
Console.WriteLine("Element is present but not clickable at the moment.");
}
System.Threading.Thread.Sleep(2000);
}
}
}