Tuesday, 18 February 2014

C# FAQ


When I click on a button on a page, a popup is displayed. This is not a windows popup. It is the application popup.. The popup I get in my application is similar to the one i have shown in the image with a X button. now How do I move the driver control to the popup and then click on the close button available on the popup and then move back my control back to the original page..
I have to do this using Selenium WebDriver and C#.
A1: You need to do the following...
·         Loop through the windows and find the desired window
·         Switch to the windows
·         Find the button in the current window and click the same
Here is the sample code in C#

foreach (string handle in browser.WindowHandles)
        {
            IWebDriver popup = driver.SwitchTo().Window(handle);

            if (popup.Title.Contains("popup title"))
            {
              break;
            }
        }

IWebElement closeButton = driver.FindElement(By.Id("closeButton"));
closeButton.Click();

A2: The example you have shown is not a popup, but a simple DHTML window. To access the X of the example you have provided, you could use: driver.findElementBy(By.id("profile-tooltip-closebtn")).click().
A3: You can try
driver.switchTo().frame(0);


 I am new to WebDriver and writing this code in C# Visual Studio (code snippet below) I am verifying if a text field is present on the home page using IsElementPresent. I get the error The name IsElementPresent does not exist in the current context. What am i doing wrong?

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Threading;
using System.Linq;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
 
namespace Homepage_check2
{
    [TestFixture]
    public class Driver
    {
        IWebDriver driver;
 
        [SetUp]
        public void Setup()
        {
            // Create a new instance of the Firefox driver
            driver = new FirefoxDriver();
        }
 
        [TearDown]
        public void Teardown()
        {
            driver.Quit();
        }
 
        [Test]
        public void homepage()
        {
            //Navigate to the site
            driver.Navigate().GoToUrl("http://www.milkround.com");
 
           Assert.IsTrue(IsElementPresent(By.Id("ctl00_uxToolbar_uxQueryTextBoxToolbar")));
 
        }
        catch
        {
            //verificationErrors.Append(e.Message);
        }
 
        }
    }
}


A1: Where does "IsElementPresent" come from? I have never seen that used in WebDriver.
In WebDriver you need to do wrap a try catch around the findElement method.
e.g
Boolean elementDisplayed;
 
try {
 
WebElement element = driver.findElement(By.Id("ctl00_uxToolbar_uxQueryTextBoxToolbar"));
elementDisplayed = element.displayed; 
 
}
catch (NoSuchElementException e) {
elementDisplayed = false;
}
Obviously you can wrap this in a helper method of some kind of perhaps add it to the WebDriver classes.
I'll leave that to you, but this is the general idea


No comments:

Post a Comment