diff --git a/examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs b/examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs index b8773c4e0eae..751a2bd04b0b 100644 --- a/examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs +++ b/examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs @@ -1,9 +1,84 @@ +// 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 System.Collections.Generic; +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 + // Click the link to activate the 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(SeleniumExtras.WaitHelpers.ExpectedConditions.AlertIsPresent()); + 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.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.AlertIsPresent()); + 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.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.AlertIsPresent()); + 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..bfd7cc688c07 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,10 @@ 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; + {{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L39-L51" >}} +{{< /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 >}} @@ -84,22 +75,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#L54-L64" >}} +{{< /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 +122,11 @@ 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()); +{{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L66-L78" >}} +{{< /tab >}} -//Type your message -alert.SendKeys("Selenium"); -//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..196dead2fb84 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#L39-L51" >}} +{{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L15-L22" >}} {{< /tab >}} @@ -79,22 +69,9 @@ 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; - -//Press the Cancel button -alert.Dismiss(); - {{< /tab >}} + {{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L54-L64" >}} +{{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L28-L35" >}} {{< /tab >}} @@ -136,19 +113,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#L66-L78" >}} +{{< /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..f20f8ac112b6 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#L39-L51" >}} +{{< /tab >}} {{< tab header="Ruby" text=true >}} {{< gh-codeblock path="examples/ruby/spec/interactions/alerts_spec.rb#L15-L22" >}} {{< /tab >}} @@ -70,24 +72,8 @@ 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; - -//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 header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L54-L64" >}} {{< /tab >}} {{< tab header="JavaScript" text=true >}} {{< gh-codeblock path="examples/javascript/test/interactions/alert.spec.js#L30-L32" >}} @@ -129,19 +115,11 @@ 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()); +{{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L66-L78" >}} +{{< /tab >}} -//Type your message -alert.SendKeys("Selenium"); -//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..7f4b24008fe8 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#L39-L51" >}} +{{< /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#L54-L64" >}} +{{< /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,11 @@ 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()); +{{< tab header="CSharp" text=true >}} +{{< gh-codeblock path="examples/dotnet/SeleniumDocs/Interactions/AlertsTest.cs#L66-L78" >}} +{{< /tab >}} -//Type your message -alert.SendKeys("Selenium"); -//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 >}}