Why Driver.SwitchTo not always works in selenium? - c#

I use the code
windowHandles = SeleniumHelper.WindowHandles();
// click...
if (SeleniumHelper.WindowHandles().Count > windowHandles.Count)
{
windowHandles = SeleniumHelper.WindowHandles();
while (pageTitle == SeleniumHelper.Driver.Title)
{
SeleniumHelper.Driver.SwitchTo().Window(windowHandles[windowHandles.Count - 1]);
Thread.Sleep(2000);
}
// do something...
SeleniumHelper.Driver.Close();
SeleniumHelper.BackToMainWindow();
}
The problem is that the driver finds the window, but does not switch to it.
Maybe there is a different way to switch to another window, like switch by javascript?

The problem is in
SeleniumHelper.Driver.SwitchTo().Window(windowHandles[windowHandles.Count - 1]);
You always switch to the last window regardless the while loop condition. Try this
string currentWindoe = SeleniumHelper.Driver.CurrentWindowHandle();
while (pageTitle != SeleniumHelper.Driver.Title)
{
SeleniumHelper.Driver.SwitchTo().Window(SeleniumHelper.Driver.CurrentWindowHandle());
Thread.Sleep(2000);
}
Or
string currentWindow = SeleniumHelper.Driver.CurrentWindowHandle();
foreach (string window in SeleniumHelper.Driver.WindowHandles())
{
if (!window.equals(currentWindow))
{
SeleniumHelper.Driver.SwitchTo().Window(window));
}
}

Related

How can I prevent C# and Selenium from exiting code 0

while (1 == 1)
{
int questions = 0;
Thread.Sleep(500);
try
{
driver.FindElement(By.XPath("//*[#id='root']/div/main/div[2]/div/div[1]/button[1]"));
questions++;
}
catch (Exception e)
{
return;
}
try
{
driver.FindElement(By.XPath("//*[#id='root']/div/main/div[2]/div/div[1]/button[2]"));
questions++;
}
catch (Exception e)
{
return;
}
try
{
driver.FindElement(By.XPath("//*[#id='root']/div/main/div[2]/div/div[1]/button[3]"));
questions++;
}
catch (Exception e)
{
return;
}
try
{
driver.FindElement(By.XPath("//*[#id='root']/div/main/div[2]/div/div[1]/button[4]"));
questions++;
}
catch (Exception e)
{
return;
}
if (questions == 0)
{
return;
}
else if (questions == 1)
{
MessageBox.Show("1", "1");
driver.FindElement(By.XPath("//*[#id='root']/div/main/div[2]/div/div[1]/button[1]")).Click();
}
else if (questions == 2)
{
MessageBox.Show("2", "2");
String[] av2 = { "//*[#id='root']/div/main/div[2]/div/div[1]/button[1]",
"//*[#id='root']/div/main/div[2]/div/div[1]/button[2]"};
Random random = new Random();
int a = random.Next(av2.Length);
driver.FindElement(By.XPath(av2[a])).Click();
}
else if (questions == 3)
{
MessageBox.Show("3", "3");
String[] av3 =
{
"//*[#id='root']/div/main/div[2]/div/div[1]/button[1]",
"//*[#id='root']/div/main/div[2]/div/div[1]/button[2]",
"//*[#id='root']/div/main/div[2]/div/div[1]/button[3]"
};
Random random = new Random();
int a = random.Next(av3.Length);
driver.FindElement(By.XPath(av3[a])).Click();
}
else if (questions == 4)
{
MessageBox.Show("4", "4");
String[] av4 =
{
"//*[#id='root']/div/main/div[2]/div/div[1]/button[1]",
"//*[#id='root']/div/main/div[2]/div/div[1]/button[2]",
"//*[#id='root']/div/main/div[2]/div/div[1]/button[3]",
"//*[#id='root']/div/main/div[2]/div/div[1]/button[4]"
};
Random random = new Random();
int a = random.Next(av4.Length);
driver.FindElement(By.XPath(av4[a])).Click();
}
Console.WriteLine(questions + " <");
}
I'm not experienced at all, and don't know why this class keeps exiting with code 0. I've tried multiple things, but none worked. So, what I am trying to have it do is have this piece of C# code check for an xpath with selenium every .5 seconds. but as it is right now, it exits with exit code 0. How can i fix this?
return statement, if placed in the Main function, stops execution of the program. Move your code to function, and add some logic to prevent console application from closing
using System;
namespace ConsoleApp7
{
class Program
{
static void Main(string[] args)
{
DoYourThings();
Console.WriteLine("Press any key to close application...");
Console.ReadKey(); // application hangs here until user clicks any button,
// so you have time to examine console window
}
static void DoYourThings()
{
while (true)
{
try
{
// some code
}
catch (Exception e)
{
Console.WriteLine(e.Message); // output exception message
return; // exits DoYourThings function
}
}
}
}
}
It appears that you want to select a random item from several XPaths (if found) and click it. The problem is that instead of ignoring exceptions (by not doing anything) you're calling return;, which will exit the method.
Instead, you can just remove the return statement from inside your catch blocks, and that should resolve your issue.
Additionally, you seem to have a lot of repeated code which can be simplified if we store some of the results in lists and then loop over those lists (and choose a random element from a list). Also, you only need to initialize the instance of Random once.
For example:
// Only initialize random one time
var random = new Random();
// Store our XPaths in a list so we can loop over them
var buttonXPaths = new List<string>
{
$"//*[#id='root']/div/main/div[2]/div/div[1]/button[1]",
$"//*[#id='root']/div/main/div[2]/div/div[1]/button[2]",
$"//*[#id='root']/div/main/div[2]/div/div[1]/button[3]",
$"//*[#id='root']/div/main/div[2]/div/div[1]/button[4]",
};
// Endless loop
while (true)
{
// Sleep for half a second
Thread.Sleep(500);
// Create a list to hold any WebElements we find
var elements = new List<WebElement>();
// For each XPath, try to find the element and add it to our list
foreach (var buttonXPath in buttonXPaths)
{
try
{
elements.Add(driver.FindElement(buttonXPath));
}
catch
{
// Ignore any exceptions. The next line isn't necessary, but I
// included it so you can see the syntax for explicitly skipping
// a loop iteration and starting it over again (you were using 'return')
continue;
}
}
// If we found any elements. This is the same as: if (elements.Count > 0)
if (elements.Any())
{
// Display the number of elements found in a message box
MessageBox.Show(elements.Count.ToString(), elements.Count.ToString());
// Choose a random element from our list
WebElement randomElement = elements[random.Next(elements.Count)];
// Click it
randomElement.Click();
// This was in your code, not sure why
Console.WriteLine($"{elements.Count} <");
}
}

How to run/cancel an event if text is not changed during a certain amount of time? C#

I am trying to run a method after a certain time if a text is not changed and if it is changed then I wont the "timer" to restart.
This is what I am currently working with.
First I have a while loop that constantly calls my TextChangedevent if any text has been added:
TextChanged(result); //result is the added text
Then I recieve it here:
string result = "";
public async void TextChanged(string text)
{
result = result + text;
}
And I keep adding text to the string. Now what I am trying to achieve is if new text does not get added within 5 seconds I want to run a function with the end result of the text:
public event EventHandler<EventArgsMethod> textChanged;
textChanged?.Invoke(this, new EventArgsMethod(result));
Now I am trying to put all the pieces together here and what I tried to do that I thought would work in theory (but a bad solution) was this code:
string result = "";
int timercheck = 0;
public async void TextChanged(string text)
{
if (result.Split(' ').Last() == text)
{
if (timercheck == 0)
{
await Task.Delay(1500);
timercheck = 1;
TextChanged (null);
}
else if (timercheck == 1)
{
await Task.Delay(1500);
timercheck = 2;
TextChanged (null);
}
else if (timercheck == 2)
{
await Task.Delay(1500);
timercheck = 3;
TextChanged (null);
}
else
{
textChanged?.Invoke(this, new EventArgsMethod(result));
TextChanged (null);
}
}
else
{
result = result + text;
}
}
So what I tried to do was to recall the method to see if new text has been added but this bad solution gives me a crash because it gets called like 20 times in matter of seconds.
How can run an event if a text is not changed during a certain amount of time? I read something about System.Timers that might be the thing I am looking for?

Opera - driver.WindowHandles returns wrong count

In my scenario, I'm verifying whether clicking on a link navigates to another page (verifying for the page title). IE, FF and chrome return 2 as expected but Opera returns 4. I didn't have any other Opera instances opened at the time of running tests. It clicks on the link and required page is opened but WindowHandles returns 4.
Code:
string BaseWindow = Drivers._driverInstance.CurrentWindowHandle;
Drivers._driverInstance.SwitchTo().Frame(Drivers._driverInstance.FindElement(By.ClassName("iframe-fix")));
if (Drivers._driverInstance.GetType().Name.ToString() == "InternetExplorerDriver")
{
IJavaScriptExecutor js = (IJavaScriptExecutor)Drivers._driverInstance;
js.ExecuteScript("arguments[0].click();", Drivers._driverInstance.FindElement(By.LinkText("Professional Services.")));
}
else
{
Drivers._driverInstance.FindElement(By.LinkText("Professional Services.")).Click();
}
System.Collections.ObjectModel.ReadOnlyCollection<string> handles = Drivers._driverInstance.WindowHandles;
if (handles.Count == 2)
{
foreach (string handle in handles)
{
if (handle != BaseWindow)
{
string title = Drivers._driverInstance.SwitchTo().Window(handle).Title;
Assert.AreEqual("title of the page", Drivers._driverInstance.Title);
}
}
}
else
{
Assert.Fail("WindowHandles returns " + handles.Count + " instead of 2");
}
Drivers._driverInstance.SwitchTo().Window(BaseWindow);
Can someone suggest why Opera returns 4 instead of 2.
Thanks.
The Opera driver doesn't return the right number of handles. This issue has already been reported to the project but it seems that the project is no longer maintained:
https://github.com/operasoftware/operachromiumdriver/issues/15
I encountered the same thing as you with Opera driver, plus (if I remember it right), the CurrentWindowHandle property doesn't work either.
Workaround:
public static void SwitchToPopup(TestTarget target, bool toPopup)
{
if (target.IsOpera)
{
if (toPopup)
{
_windowIndex += 3;
new WebDriverWait(target.Driver, TimeSpan.FromSeconds(DefaultTimeoutInSeconds)).Until(d => d.WindowHandles.Count > _windowIndex);
}
else
{
_windowIndex -= 3;
}
target.Driver.SwitchTo().Window(target.Driver.WindowHandles[_windowIndex]);
}
else
{
IEnumerable<string> windowHandles = toPopup ? target.Driver.WindowHandles : target.Driver.WindowHandles.Reverse();
bool bFound = false;
foreach (string windowHandle in windowHandles)
{
if (bFound)
{
target.Driver.SwitchTo().Window(windowHandle);
break;
}
bFound = windowHandle == target.Driver.CurrentWindowHandle;
}
}
}

How to leave a while loop inside a foreach loop

I have a while loop and inside this while loop I have a foreach loop.
Here I learned how to skip the currently interaction on a loop by using Continue; Return; Break. But I need to leave the while loop when i'm inside the foreach loop is that possible ?
I'm on a interaction inside of the foreach loop and I want to leave the foreach and go to the next interaction of the while. How May I do that ?
Like so:
while (!reader.EndOfStream) //COMEÇO DO WHILE
{
///Some Codes
foreach(string s in checkedlistbox1.items)
{
switch(s)
{
case "1":
if( 1 > 0)
{
///HERE I WANT TO GO TO THE NEXT INTERACTION OF THE WHILE
///When I use CONTINUE; HERE, I GO TO THE NEXT INTERACTION OF THE FOREACH. BUT I NEED TO GO TO THE NEXT OF THE WHILE.
}
}
}
}
Here's what I want do to:
I'm reading a file.txt line by line, and writing a new one with these values and some others things... Some of these values may be required (seted by user), if a required fieldis empty, so I do nothing, and go to the next while interaction...
You need to break from the switch and then from the foreach, whilst having a variable set.
You can then check that variable to see whether you should continue to the next while iteration.
Do it like this:
while (!reader.EndOfStream)
{
// Some Codes
bool skipToNext = false;
foreach (string s in checkedlistbox1.items)
{
switch (s)
{
case "1":
if (1 > 0)
{
skipToNext = true;
break;
}
}
if (skipToNext) break;
}
// in the case of there being more code, you can now use continue
if (skipToNext) continue;
// more code
}
Example of this flow working
var list = new List<string> { "0", "1", "2" };
int a = 0, b = 2;
while (a++ < b)
{
// Some Codes
bool skipToNext = false;
foreach (string s in list)
{
Debug.WriteLine("{0} - {1}", a, s);
switch (s)
{
case "1":
if (1 > 0)
{
Debug.WriteLine("Skipping switch...");
skipToNext = true;
break;
}
}
if (skipToNext)
{
Debug.WriteLine("Skipping foreach...");
break;
}
}
// in the case of there being more code, you can now use continue
if (skipToNext)
{
Debug.WriteLine("Skipping to next while...");
continue;
}
// more code
}
Outputs:
1 - 0
1 - 1
Skipping switch...
Skipping foreach...
Skipping to next while...
2 - 0
2 - 1
Skipping switch...
Skipping foreach...
Skipping to next while...
bool dobreak = false;
while (!reader.EndOfStream && !dobreak ) //COMEÇO DO WHILE
{
///Some Codes
foreach(string s in checkedlistbox1.items)
{
switch(s)
{
case "1":
if( 1 > 0)
{
dobreak = true;
break;
}
}
}
}
1) Yes, it is possible. It works.
foreach() {
while() {
break; //leave the while
}
//... and continues from here
}
2) Each while will end before the next foreach iteration, so the second question does not make much sense... Unless you mean starting the next while inside the foreach, in which case.. yes!
foreach() {
while() {
...
}
break; //will go to the next foreach iteration, i.e. starts a new while
}
As for your code sample, a break in the point you mentioned in the comment will do what you need (exit the foreach, going naturally to the next while iteration).
EDIT: after you posted the example, it appears that your problem is not in the interaction between while and foreach, but in the switch: break is used as a keyword for both "go to the next iteration in the loop" and "finish this case and the switch".
The break will be seen by the compiler as a 'switch break'. You need to mimic the behavior by yourself:
foreach(string s in checkedlistbox1.items)
{
bool dobreak = false;
switch(s)
{
case "1":
if( 1 > 0)
{
dobreak = true;
}
break; // exit the case
}
if (dobreak)
break; // exits the for (it is a 'foreach-break')
}
Not tested, but these are a few possible ways which should answer your question:
bool continueLooping = true;
string[] array = { "1", "2" };
while (continueLooping)
{
foreach (string x in array)
{
// break out of foreach loop AND while loop
if (x == "1")
{
continueLooping = false;
break;
}
// go to next iteration of foreach loop
if (x == "2")
{
continue;
}
// break out of foreach loop and continue while loop
if (x == "3")
{
break;
}
}
}
You need to break from your foreach and the while so something like this:
bool abort = false;
while (!reader.EndOfStream && !abort) //COMEÇO DO WHILE
{
///Some Codes
foreach(string s in checkedlistbox1.items)
{
switch(s)
{
case "1":
if( 1 > 0)
{
///HERE I WANT TO LEAVE TO THE NEXT INTERACTION OF THE WHILE
abort = true;
break;
}
}
if (abort)
{
break;
}
}
}
I'm beginner in C#, but I suggest that you add another condition to the while loop that you can set yourself. Then, change it whenever is needed. For example:
MyKeyCondition = 0;
while(MainCondition||MyKeyCondition){
if(MyCheckCondition){
MyKeyCondition = 1;
}
}
Now, if you change your key condition, you can handle the while loop even if the main condition is not satisfied.
May try goto;
while (true)
{
foreach(string s in checkedlistbox1.items)
{
switch(s)
{
case "1":
if( 1 > 0)
{
goto WhileOut;
}
}
}
}
WhileOut:
// some code
Often nested loops are an indication that a method should be split up.
In this example, it could make the code clearer. If you place the inner for loop into a separate method, you can make that method return a bool. Return true if you want the outer while loop to continue iterating; otherwise, return false if you want it to exit the while loop.
It would look something like this:
private void doSomething(StreamReader reader)
{
while (!reader.EndOfStream)
{
// Some Codes...
if (!processNextItem(reader))
{
break;
}
}
}
// Returns true if the caller should continue its while loop; false if it should exit it.
private bool processNextItem(StreamReader reader)
{
foreach (string s in checkedlistbox1.items)
{
switch (s)
{
case "1":
if (1 > 0)
{
return false; // Return false to exit while loop.
}
}
}
return true; // Return true to continue while loop.
}
I'm not sure what you'd need to pass to processNextItem(); I've just passed reader as an example.

'Advanced' Console Application

I'm not sure if this question has been answered elsewhere and I can't seem to find anything through google that isn't a "Hello World" example... I'm coding in C# .NET 4.0.
I'm trying to develop a console application that will open, display text, and then wait for the user to input commands, where the commands will run particular business logic.
For example: If the user opens the application and types "help", I want to display a number of statements etc etc. I'm not sure how to code the 'event handler' for user input though.
Hopefully this makes sense. Any help would be much appreciated!
Cheers.
You need several steps to achieve this but it shouldn't be that hard. First you need some kind of parser that parses what you write. To read each command just use var command = Console.ReadLine(), and then parse that line. And execute the command... Main logic should have a base looking this (sort of):
public static void Main(string[] args)
{
var exit = false;
while(exit == false)
{
Console.WriteLine();
Console.WriteLine("Enter command (help to display help): ");
var command = Parser.Parse(Console.ReadLine());
exit = command.Execute();
}
}
Sort of, you could probably change that to be more complicated.
The code for the Parser and command is sort of straight forward:
public interface ICommand
{
bool Execute();
}
public class ExitCommand : ICommand
{
public bool Execute()
{
return true;
}
}
public static Class Parser
{
public static ICommand Parse(string commandString) {
// Parse your string and create Command object
var commandParts = commandString.Split(' ').ToList();
var commandName = commandParts[0];
var args = commandParts.Skip(1).ToList(); // the arguments is after the command
switch(commandName)
{
// Create command based on CommandName (and maybe arguments)
case "exit": return new ExitCommand();
.
.
.
.
}
}
}
I know this is an old question, but I was searching for an answer too. I was unable to find a simple one though, so I built InteractivePrompt. It's available as a NuGet Package and you can easily extend the code which is on GitHub. It features a history for the current session also.
The functionality in the question could be implemented this way with InteractivePrompt:
static string Help(string strCmd)
{
// ... logic
return "Help text";
}
static string OtherMethod(string strCmd)
{
// ... more logic
return "Other method";
}
static void Main(string[] args)
{
var prompt = "> ";
var startupMsg = "BizLogic Interpreter";
InteractivePrompt.Run(
((strCmd, listCmd) =>
{
string result;
switch (strCmd.ToLower())
{
case "help":
result = Help(strCmd);
break;
case "othermethod":
result = OtherMethod(strCmd);
break;
default:
result = "I'm sorry, I don't recognize that command.";
break;
}
return result + Environment.NewLine;
}), prompt, startupMsg);
}
This is easy enough, just use the Console.WriteLine and Console.ReadLine() methods. From the ReadLine you get a string. You could have a horrible if statement that validate this against known/expected inputs. Better would be to have a lookup table. Most sophisticated would be to write a parser. It really depends on how complex the inputs can be.
Console.WriteLine Console.ReadLine and Console.ReadKey are your friends. ReadLine and ReadKey waits for user input. The string[] args will have all of your parameters such as 'help' in them. The array is created by separating the command line arguments by spaces.
switch (Console.ReadLine())
{
case "Help":
// print help
break;
case "Other Command":
// do other command
break;
// etc.
default:
Console.WriteLine("Bad Command");
break;
}
If you're looking to parse commands that have other things in them like parameters, for example "manipulate file.txt" then this alone won't work. But you could for example use String.Split to separate the input into a command and arguments.
A sample:
static void Main(string[] args)
{
Console.WriteLine("Welcome to test console app, type help to get some help!");
while (true)
{
string input = Console.ReadLine();
int commandEndIndex = input.IndexOf(' ');
string command = string.Empty;
string commandParameters = string.Empty;
if (commandEndIndex > -1)
{
command = input.Substring(0, commandEndIndex);
commandParameters = input.Substring(commandEndIndex + 1, input.Length - commandEndIndex - 1);
}
else
{
command = input;
}
command = command.ToUpper();
switch (command)
{
case "EXIT":
{
return;
}
case "HELP":
{
Console.WriteLine("- enter EXIT to exit this application");
Console.WriteLine("- enter CLS to clear the screen");
Console.WriteLine("- enter FORECOLOR value to change text fore color (sample: FORECOLOR Red) ");
Console.WriteLine("- enter BACKCOLOR value to change text back color (sample: FORECOLOR Green) ");
break;
}
case "CLS":
{
Console.Clear();
break;
}
case "FORECOLOR":
{
try
{
Console.ForegroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), commandParameters);
}
catch
{
Console.WriteLine("!!! Parameter not valid");
}
break;
}
case "BACKCOLOR":
{
try
{
Console.BackgroundColor = (ConsoleColor)Enum.Parse(typeof(ConsoleColor), commandParameters);
}
catch
{
Console.WriteLine("!!! Parameter not valid");
}
break;
}
default:
{
Console.WriteLine("!!! Bad command");
break;
}
}
}
}
This is very simplistic, but might meet your needs.
// somewhere to store the input
string userInput="";
// loop until the exit command comes in.
while (userInput != "exit")
{
// display a prompt
Console.Write("> ");
// get the input
userInput = Console.ReadLine().ToLower();
// Branch based on the input
switch (userInput)
{
case "exit":
break;
case "help":
{
DisplayHelp();
break;
}
case "option1":
{
DoOption1();
break;
}
// Give the user every opportunity to invoke your help system :)
default:
{
Console.WriteLine ("\"{0}\" is not a recognized command. Type \"help\" for options.", userInput);
break;
}
}
}
There is a C# nuget package called 'ReadLine' by 'tornerdo'. The statement ReadLine.Read(" prompt > "); prompts the user within options provided in CustomAutoCompletionHandler.PossibleAutoCompleteValues.
Additionally, you can change the CustomAutoCompletionHandler.PossibleAutoCompleteValues for each prompt. This ensures that the user get to choose an option from available\ supported list of options. Less error prone.
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(" Note! When it prompts, press <tab> to get the choices. Additionally, you can use type ahead search.");
Console.ForegroundColor = ConsoleColor.White;
// Register auto completion handler..
ReadLine.AutoCompletionHandler = new CustomAutoCompletionHandler();
CustomAutoCompletionHandler.PossibleAutoCompleteValues = new List<string> { "dev", "qa", "stg", "prd" };
var env = CoverReadLine(ReadLine.Read(" Environment > "));
Console.WriteLine($"Environment: {env}");
}
private static string CoverReadLine(string lineRead) => CustomAutoCompletionHandler.PossibleAutoCompleteValues.Any(x => x == lineRead) ? lineRead : throw new Exception($"InvalidChoice. Reason: No such option, '{lineRead}'");
public class CustomAutoCompletionHandler : IAutoCompleteHandler
{
public static List<string> PossibleAutoCompleteValues = new List<string> { };
// characters to start completion from
public char[] Separators { get; set; } = new char[] { ' ', '.', '/' };
// text - The current text entered in the console
// index - The index of the terminal cursor within {text}
public string[] GetSuggestions(string userText, int index)
{
var possibleValues = PossibleAutoCompleteValues.Where(x => x.StartsWith(userText, StringComparison.InvariantCultureIgnoreCase)).ToList();
if (!possibleValues.Any()) possibleValues.Add("InvalidChoice");
return possibleValues.ToArray();
}
}

Categories