diff --git a/examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs b/examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs index b8773c4e0eae..c3ef779a2553 100644 --- a/examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs +++ b/examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs @@ -1,9 +1,109 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +using System; using Microsoft.VisualStudio.TestTools.UnitTesting; +using OpenQA.Selenium; +using OpenQA.Selenium.Chrome; +using OpenQA.Selenium.Support.UI; namespace SeleniumDocs.Interactions { [TestClass] - public class AlertsTest : BaseTest + public class AlertsTest { + [TestMethod] + public void TestAlertCommands(){ + WebDriver driver = new ChromeDriver(); + driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromMilliseconds(500); + // Navigate to Url + driver.Url= "https://www.selenium.dev/documentation/webdriver/interactions/alerts/"; + // Simple Alert + IJavaScriptExecutor js = (IJavaScriptExecutor)driver; + // Execute JS for alert + js.ExecuteScript("alert('Sample Alert');"); + WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30)); + // Wait for the alert to be displayed and store it in a variable + wait.Until((driver) => { + try + { + return driver.SwitchTo().Alert(); + } + catch (Exception ex) + { + return null; + } + }); + + IAlert alert = driver.SwitchTo().Alert(); + // Store the alert text in a variable and verify it + string text = alert.Text; + Assert.AreEqual(text, "Sample Alert"); + alert.Accept(); + + // Confirm Alert + // Execute JS for confirm + js.ExecuteScript("confirm('Are you sure?');"); + // Wait for the alert to be displayed + // Wait for the alert to be displayed and store it in a variable + wait.Until((driver) => { + try + { + return driver.SwitchTo().Alert(); + } + catch (Exception ex) + { + return null; + } + }); + + alert = driver.SwitchTo().Alert(); + // Store the alert text in a variable and verify it + text = alert.Text; + Assert.AreEqual(text, "Are you sure?"); + // Press the Cancel button + alert.Dismiss(); + + // Prompt Alert + // Execute JS for prompt + js.ExecuteScript("prompt('What is your name?');"); + // Wait for the alert to be displayed + // Wait for the alert to be displayed and store it in a variable + wait.Until((driver) => { + try + { + return driver.SwitchTo().Alert(); + } + catch (Exception ex) + { + return null; + } + }); + + alert = driver.SwitchTo().Alert(); + // Store the alert text in a variable and verify it + text = alert.Text; + Assert.AreEqual(text, "What is your name?"); + // Type your message + alert.SendKeys("Selenium"); + // Press the OK button + alert.Accept(); + + //quitting driver + driver.Quit(); //close all windows + } } } \ No newline at end of file diff --git a/website_and_docs/content/documentation/webdriver/interactions/alerts.en.md b/website_and_docs/content/documentation/webdriver/interactions/alerts.en.md index 8ddf4967592e..51bb1d3ecdb7 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/alerts.en.md +++ b/website_and_docs/content/documentation/webdriver/interactions/alerts.en.md @@ -33,19 +33,9 @@ alerts. {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}} {{< /tab >}} - {{< tab header="CSharp" >}} -//Click the link to activate the alert -driver.FindElement(By.LinkText("See an example alert")).Click(); - -//Wait for the alert to be displayed and store it in a variable -IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent()); - -//Store the alert text in a variable -string text = alert.Text; - -//Press the OK button -alert.Accept(); - {{< /tab >}} + {{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L51-L55" >}} +{{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L15-L22" >}} {{< /tab >}} @@ -84,22 +74,10 @@ This example also shows a different approach to storing an alert: {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}} {{< /tab >}} - {{< tab header="CSharp" >}} -//Click the link to activate the alert -driver.FindElement(By.LinkText("See a sample confirm")).Click(); - -//Wait for the alert to be displayed -wait.Until(ExpectedConditions.AlertIsPresent()); - -//Store the alert in a variable -IAlert alert = driver.SwitchTo().Alert(); - -//Store the alert in a variable for reuse -string text = alert.Text; + {{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L73-L78" >}} +{{< /tab >}} -//Press the Cancel button -alert.Dismiss(); - {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L28-L35" >}} {{< /tab >}} @@ -143,19 +121,10 @@ See a sample prompt. {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}} {{< /tab >}} - {{< tab header="CSharp" >}} -//Click the link to activate the alert -driver.FindElement(By.LinkText("See a sample prompt")).Click(); - -//Wait for the alert to be displayed and store it in a variable -IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent()); - -//Type your message -alert.SendKeys("Selenium"); + {{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L96-L103" >}} +{{< /tab >}} -//Press the OK button -alert.Accept(); - {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L41-L48" >}} {{< /tab >}} diff --git a/website_and_docs/content/documentation/webdriver/interactions/alerts.ja.md b/website_and_docs/content/documentation/webdriver/interactions/alerts.ja.md index eed2c39bda59..ff8034f8f4bc 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/alerts.ja.md +++ b/website_and_docs/content/documentation/webdriver/interactions/alerts.ja.md @@ -29,19 +29,9 @@ WebDriverはポップアップからテキストを取得し、これらのア {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}} {{< /tab >}} - {{< tab header="CSharp" >}} -//Click the link to activate the alert -driver.FindElement(By.LinkText("See an example alert")).Click(); - -//Wait for the alert to be displayed and store it in a variable -IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent()); - -//Store the alert text in a variable -string text = alert.Text; - -//Press the OK button -alert.Accept(); - {{< /tab >}} + {{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L51-L55" >}} +{{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L15-L22" >}} {{< /tab >}} @@ -79,22 +69,10 @@ alert.accept() {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}} {{< /tab >}} - {{< tab header="CSharp" >}} -//Click the link to activate the alert -driver.FindElement(By.LinkText("See a sample confirm")).Click(); - -//Wait for the alert to be displayed -wait.Until(ExpectedConditions.AlertIsPresent()); - -//Store the alert in a variable -IAlert alert = driver.SwitchTo().Alert(); - -//Store the alert in a variable for reuse -string text = alert.Text; + {{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L73-L78" >}} +{{< /tab >}} -//Press the Cancel button -alert.Dismiss(); - {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L28-L35" >}} {{< /tab >}} @@ -136,19 +114,10 @@ alert.dismiss() {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}} {{< /tab >}} - {{< tab header="CSharp" >}} -//Click the link to activate the alert -driver.FindElement(By.LinkText("See a sample prompt")).Click(); - -//Wait for the alert to be displayed and store it in a variable -IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent()); - -//Type your message -alert.SendKeys("Selenium"); + {{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L96-L103" >}} +{{< /tab >}} -//Press the OK button -alert.Accept(); - {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L41-L48" >}} {{< /tab >}} diff --git a/website_and_docs/content/documentation/webdriver/interactions/alerts.pt-br.md b/website_and_docs/content/documentation/webdriver/interactions/alerts.pt-br.md index e741c7e2ab78..a027ecdeec58 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/alerts.pt-br.md +++ b/website_and_docs/content/documentation/webdriver/interactions/alerts.pt-br.md @@ -31,7 +31,9 @@ alertas. {{< tab header="Python" text=true >}} {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}} {{< /tab >}} - + {{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L51-L55" >}} +{{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L15-L22" >}} {{< /tab >}} @@ -70,22 +72,10 @@ Este exemplo também mostra uma abordagem diferente para armazenar um alerta: {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}} {{< /tab >}} - {{< tab header="CSharp" >}} -//Click the link to activate the alert -driver.FindElement(By.LinkText("See a sample confirm")).Click(); - -//Wait for the alert to be displayed -wait.Until(ExpectedConditions.AlertIsPresent()); - -//Store the alert in a variable -IAlert alert = driver.SwitchTo().Alert(); - -//Store the alert in a variable for reuse -string text = alert.Text; + {{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L73-L78" >}} +{{< /tab >}} -//Press the Cancel button -alert.Dismiss(); - {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L28-L35" >}} {{< /tab >}} @@ -129,19 +119,10 @@ Veja um exemplo de prompt . {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}} {{< /tab >}} - {{< tab header="CSharp" >}} -//Click the link to activate the alert -driver.FindElement(By.LinkText("See a sample prompt")).Click(); - -//Wait for the alert to be displayed and store it in a variable -IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent()); - -//Type your message -alert.SendKeys("Selenium"); + {{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L96-L103" >}} +{{< /tab >}} -//Press the OK button -alert.Accept(); - {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L41-L48" >}} {{< /tab >}} diff --git a/website_and_docs/content/documentation/webdriver/interactions/alerts.zh-cn.md b/website_and_docs/content/documentation/webdriver/interactions/alerts.zh-cn.md index b7986383e8f3..a1420ad0c682 100644 --- a/website_and_docs/content/documentation/webdriver/interactions/alerts.zh-cn.md +++ b/website_and_docs/content/documentation/webdriver/interactions/alerts.zh-cn.md @@ -26,19 +26,10 @@ WebDriver可以从弹窗获取文本并接受或关闭这些警告. {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L12-L18" >}} {{< /tab >}} - {{< tab header="CSharp" >}} -//Click the link to activate the alert -driver.FindElement(By.LinkText("See an example alert")).Click(); - -//Wait for the alert to be displayed and store it in a variable -IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent()); - -//Store the alert text in a variable -string text = alert.Text; + {{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L51-L55" >}} +{{< /tab >}} -//Press the OK button -alert.Accept(); - {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L15-L22" >}} {{< /tab >}} @@ -75,22 +66,10 @@ alert.accept() {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L26-L32" >}} {{< /tab >}} - {{< tab header="CSharp" >}} -//Click the link to activate the alert -driver.FindElement(By.LinkText("See a sample confirm")).Click(); - -//Wait for the alert to be displayed -wait.Until(ExpectedConditions.AlertIsPresent()); - -//Store the alert in a variable -IAlert alert = driver.SwitchTo().Alert(); - -//Store the alert in a variable for reuse -string text = alert.Text; + {{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L73-L78" >}} +{{< /tab >}} -//Press the Cancel button -alert.Dismiss(); - {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L28-L35" >}} {{< /tab >}} @@ -131,19 +110,10 @@ alert.dismiss() {{< gh-codeblock path="examples/python/tests/interactions/test_alerts.py#L40-L47" >}} {{< /tab >}} - {{< tab header="CSharp" >}} -//Click the link to activate the alert -driver.FindElement(By.LinkText("See a sample prompt")).Click(); - -//Wait for the alert to be displayed and store it in a variable -IAlert alert = wait.Until(ExpectedConditions.AlertIsPresent()); - -//Type your message -alert.SendKeys("Selenium"); + {{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L96-L103" >}} +{{< /tab >}} -//Press the OK button -alert.Accept(); - {{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L41-L48" >}} {{< /tab >}}