-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwitchCommands.cs
73 lines (58 loc) · 2.33 KB
/
SwitchCommands.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
using OpenQA.Selenium;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SeleniumCSharp_BrowserCommands
{
internal class SwitchCommands : BrowserCommandsTests
{
[Test]
public void SwitchWindows()
{
// Get the current window’s handle
var handle = driver.CurrentWindowHandle;
// Get the list of all opened windows’ handles
var handles = driver.WindowHandles;
//Switch to the window you want active in your test
driver.SwitchTo().Window(handle);
//Switch to the window you want active in your test, by selecting it from a list
driver.SwitchTo().Window(handles[3]);
}
[Test]
public void DismissAlert()
{
// Navigate to the desired page
driver.Navigate().GoToUrl("https://www.lambdatest.com/selenium-playground/javascript-alert-box-demo");
// Identify the button to click
driver.FindElement(By.XPath("//div[contains(text(),'Java Script Alert Box')]/following-sibling::p/button")).Click();
// Dismiss the alert
driver.SwitchTo().Alert().Dismiss();
}
[Test]
public void SendKeysAndAcceptAlert()
{
// Navigate to the desired page
driver.Navigate().GoToUrl("https://www.lambdatest.com/selenium-playground/javascript-alert-box-demo");
// Identify the button to click
driver.FindElement(By.XPath("//div[contains(text(),'Java Script Alert Box')]/following-sibling::p/button")).Click();
// Enter text in the alert box
driver.SwitchTo().Alert().SendKeys("It's me, Mario");
// Accept the alert
driver.SwitchTo().Alert().Accept();
}
[Test]
public void SwitchToFrames()
{
// Navigate to the desired page
driver.Navigate().GoToUrl("https://codepen.io/fidabrj/pen/NWYeaqG");
// Switch to frame by name
driver.SwitchTo().Frame("result");
// Switch to the parent frame
driver.SwitchTo().ParentFrame();
// Switch to the first frame or the main document of the web page
driver.SwitchTo().DefaultContent();
}
}
}