Selenium webdriver with Dynamic Crm online - c#

Trying to use the webdriver to do some automated testing with crm. Im having issues with consistency. Ill run a test and it'll pass and the next time it will fail.I tried using waits and am still having this isuse. Has anyone used crm and selenium together and know any ideas on how to fix this issue?
IWebDriver driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
// Thread.Sleep(10000);
driver.Navigate().GoToUrl("https://login.microsoftonline.com/ea791863-b17d-43ec-90ba-148d3fa0a86f/wsfed?wa=wsignin1.0&wtrealm=https%3a%2f%2fmerlintest.crm.dynamics.com%2f&wctx=pr%3dwsfederation%26rm%3dhttps%253a%252f%252fmerlintest.crm.dynamics.com%252f%26ry%3dhttps%253a%252f%252fmerlintest.crm.dynamics.com%252fmain.aspx&wct=2017-07-05T20%3a42%3a50Z&wreply=https%3a%2f%2fcloudredirector.crm.dynamics.com%3a443%2fG%2fAuthRedirect%2fIndex.aspx%3fRedirectTo%3dhttps%253a%252f%252fmerlintest.crm.dynamics.com%252fmain.aspx#386670653");
IWebElement emailOrPhone = driver.FindElement(By.Id("cred_userid_inputtext")); //email or phone input
IWebElement password = driver.FindElement(By.Id("cred_password_inputtext"));//password input
emailOrPhone.SendKeys("email");
password.SendKeys("password");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("cred_sign_in_button")));
IWebElement enterButton = driver.FindElement(By.Id("cred_sign_in_button"));
enterButton.SendKeys(Keys.Enter);//login
driver.SwitchTo().DefaultContent();//switches frames ,needed this so we can grab elements
Thread.Sleep(5000);
//navigate to search box and enter candidate name
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementExists((By.ClassName("navImageFlipHorizontal"))));
driver.FindElement(By.ClassName("navImageFlipHorizontal")).Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
Thread.Sleep(5000);
driver.FindElement(By.Id("navTabButtonSearchD365Id")).SendKeys("");
driver.FindElement(By.Id("findCriteriaImg")).Click();
WebDriverWait iwait = new WebDriverWait(driver, TimeSpan.FromSeconds(25));
IWebElement element = iwait.Until(
ExpectedConditions.ElementIsVisible(By.Id("searchTextBox")));
Thread.Sleep(5000);
driver.FindElement(By.Id("searchTextBox")).Clear();
driver.FindElement(By.Id("searchTextBox")).SendKeys("lisa steinburg");
driver.FindElement(By.Id("searchTextBox")).SendKeys(Keys.Enter);

Related

c# Selenium clicking the matching result

I want to click on the video that matches the channel name according to the results after searching on youtube, but I do the channel match, but I can click the channel, not the video. i want Channel Name Or Url When it matches, its video is opened.
var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
options.AddArgument("start-maximized");
options.AddArgument("disable-infobars");
options.AddArgument("--disable-extensions");
var driver = new ChromeDriver(chromeDriverService, options);
driver.Navigate().GoToUrl("https://www.youtube.com/");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(1500));
var elementsWithSearchID = wait.Until((driver) => driver.FindElements(By.Id("search")));
var search = elementsWithSearchID.Where(e => e.TagName == "input").FirstOrDefault();
search.SendKeys("Hello\n");
Thread.Sleep(2000);
IWebElement channel = driver.FindElement(By.XPath("//*[#id=\"text\"]/a[contains(#href, '/channel/UCV1Nlv5cOSB--hEjRVo4mUA')]"));
channel.Click();
I checked your code and run it, so you want to open video which match with your channel name, right?
so you need to first take the link which match with your channel name and after that put below code .
driver.FindElement(By.XPath("//a[#href='/watch?v=VKIiCOZ2Eo4']")).Click();
instead of
IWebElement channel = driver.FindElement(By.XPath("//*[#id=\"text\"]/a[contains(#href, '/channel/UCV1Nlv5cOSB--hEjRVo4mUA')]"));

WebDriver throws timeout error when trying to access Url property

I click on button which downloads PDF file from server and opens it in new tab. After click i wait when the tab will be opened:
TimeSpan timeSpan = TimeSpan.FromSeconds(10);
WebDriverWait waiter = new WebDriverWait(driver, timeSpan);
waiter.Until(drv => drv.WindowHandles.Count == 2);
When this PDF file is opened i switch to new tab:
string handle = drv.WindowHandles.Last();
drv.SwitchTo().Window(handle);
Then i try to access Url property on web driver:
TimeSpan timeSpan = TimeSpan.FromSeconds(10);
WebDriverWait waiter = new WebDriverWait(driver, timeSpan);
waiter.Until(driver => driver.Url.StartsWith("blob:"));
But i get this error:
The HTTP request to the remote WebDriver server for URL http://localhost:44829/session/fc28864b05b7a2b3ad16fd69e29fb48d/url timed out after 60 seconds.
This issue appears only in Headless mode on linux machine:
public static IWebDriver Create()
{
ChromeOptions options = new ChromeOptions();
options.AddArgument("headless");
options.AddArgument("no-sandbox");
options.AddArgument("window-size=1920x1080");
IWebDriver driver = new ChromeDriver(options);
return driver;
}
But there are no problems in simple chrome mode on Windows:
public static IWebDriver Create()
{
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Maximize();
return driver;
}
This issue appears when trying to search elements too.

Selenium ChromeDriver - the HTTP request to the remote WebDriver server for URL timed out after 60 seconds

I am using the ChromeDriver in an nunit test to test whether a complex page loads:
public ChromeDriver Driver { get; private set; }
[OneTimeSetUp]
public void Setup()
{
ChromeOptions co = new ChromeOptions{};
co.AddArgument("no-sandbox");
Driver = new ChromeDriver( co) ;
Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(120);
Driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(120);
Driver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(120);
Driver.Manage().Window.Maximize();
}
As you can see I have tried to increase timeouts to 2 miutes everywhere yet when I run
Driver.Navigate().GoToUrl(url);
against the page I get
OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL timed out after 60 seconds.
The page does take longer than 60 seconds to load, So how do I increase the 60 seconds ?
You need to increase the DefaultCommandTimeout in RemoteWebDriver. You can do it by using the ChromeDriver(ChromeDriverService, ChromeOptions, TimeSpan) or ChromeDriver(string, ChromeOptions, TimeSpan) overloads
ChromeOptions co = new ChromeOptions{};
Driver = new ChromeDriver("path to ChromeDriver.exe", co, TimeSpan.FromSeconds(120));
// or
Driver = new ChromeDriver(ChromeDriverService.CreateDefaultService(), co, TimeSpan.FromSeconds(120));

React based form fields are not Visible to Selenium, although it is available in DOM

There are a few React based a form element in this webpage and I am getting OpenQA.Selenium.WebDriverTimeoutException for Password field, when I wait until it is available, although it is present in the DOM. Please find the code I wrote in C# using Selenium:
var Driver = new ChromeDriver();
Driver.Manage().Window.Maximize();
Driver.Url = #"https://www.crownbet.com.au";
//Opens the login flyout
Driver.FindElement(By.XPath("//button[contains(#class,'login-flyout-button__title') and contains(text(),'LOGIN')]")).Click();
//Defining Wait
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(20));
//Waiting for Username Field
wait.Until(ExpectedConditions.ElementIsVisible(By.Name("username")));
//Entering Username
Driver.FindElement(By.Name("username")).SendKeys("username");
//Waiting for Password Field- THIS FAILS, ALTHOUGH FIELD IS VISIBLE.
wait.Until(ExpectedConditions.ElementIsVisible(By.Name("password")));
//Entering Password
Driver.FindElement(By.Name("password")).SendKeys("password");
Driver.Quit();
wait.Until(ExpectedConditions.ElementIsVisible(By.Name("password")))
is throwing WebDriverTimeoutException. Can someone suggest, how to
handle this ReactJS form?
A couple of points:
The Username field and the Password field renders at the same time. So once we wait for the Username field, we don't need to wait for the Password field individually anymore.
Instead of using the ElementIsVisible clause we can use ElementToBeClickable clause.
Instead of using the name locator, we will use an unique logical xpath.
Your code block will be :
var Driver = new ChromeDriver();
Driver.Manage().Window.Maximize();
Driver.Url = #"https://www.crownbet.com.au";
//Opens the login flyout
Driver.FindElement(By.XPath("//button[contains(#class,'login-flyout-button__title') and contains(text(),'LOGIN')]")).Click();
//Defining Wait
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(20));
//Waiting for Username & Password Field
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[#name='username']")));
//Entering Username
Driver.FindElement(By.XPath("//input[#name='username']")).SendKeys("username");
//Entering Password
Driver.FindElement(By.XPath("//input[#name='password']")).SendKeys("password");
Driver.Quit();

Selenium Firefox Profile Path not taken into account

I just want to test locally. With Internet Explorer it works. With Firefox, I get a timeout on line driver.FindElement :
var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
capabilitiesInternet.SetCapability("ignoreProtectedModeSettings", true);
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl(url);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement category = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Name("login"));
});
// Login
driver.FindElement(By.Name("login")).SendKeys("test");
Error message is httpRequest to remotedriver timeout.
Upate: I think it's due to the fact that I have a portable version of Firefox 21 and an older version of FF which cannot work with Selenium whereas Selenium launch old version. So I tried to indicate the path of portable :
var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
capabilitiesInternet.SetCapability("ignoreProtectedModeSettings", true);
string path = #"C:\Portable";
FirefoxProfile ffprofile = new FirefoxProfile(path);
IWebDriver driver = new FirefoxDriver(ffprofile);
Unfortunately it keeps running the old version (I cannot change the old version because of corporate environment).
Is there anyway to make this profile work ?
Not sure if it is your problem, but in order to 'point' Selenium to where Firefox is located, you are looking for the FirefoxBinary class:
var binary = new FirefoxBinary("pathtofirefox");
string path = #"C:\Portable";
FirefoxProfile ffprofile = new FirefoxProfile(path);
var driver = new FirefoxDriver(binary, profile);

Categories

Resources