Text Contains two different strings? WebDriver C# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to assert whether two or more strings are evident. My code currently only looks for "Good". Is there a way to look for "Good" or "Bad"?
public class Test
{
public static bool FindText()
{
var conf = Driver.Instance.FindElement(By.Id("foo"));
if (conf.Text.Contains("Good"))
{
return true;
}
throw new Exception("Text not found");
}
}

I would use System.Linq and check against all elements of an array, if there could possibly be more than two valid strings.
public class Test
{
public static bool FindText()
{
var stringsToFind = new [] { "Good", "Bad" };
var conf = Driver.Instance.FindElement(By.Id("foo"));
if (stringsToFind.Any(s => conf.Text.Contains(s))
{
return true;
}
throw new Exception("Text not found");
}
}
for only two elements to check I would propably just extend the if condition with a second condition and an or.

When trying to find a string, always make the string variable to upper or lower case. Since it's case sensitive, when the text is "GoOd", you won't find a match looking for "Good"
if(conf.Text.ToUpper().Contains("GOOD")){
//do something
}
else if(conf.Text.ToUpper().Contains("BAD")){
//do something else
}
You could also put then in only one "if" statement, if you're only interested in finding out if there's any of those by using
if(conf.Text.ToUpper().Contains("GOOD") || conf.Text.ToUpper().Contains("BAD")){
//do something for both cases
}

|| is the operator for the OR operation
if (conf.Text.Contains("Good") || conf.Text.Contains("Bad"))
PD : Stop whatever you are doing and take a look to the language docs, you need to understand what are you doing.

Related

I want to fill a dictionary<string,int> with data from a txt file. The file contains names and numbers this way: Peter;123 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
What i want to do is to use something like this Console.Writeline($"Name {Key} - Points: {Value}");
but i dont know how to make the data from the txt into Keys and Values.
I see, you want to fill a Dictionary<string, int> with the names and points of people, right?
If yes then I would suggest never use a dictionary Keys for names, because names are the data that can be duplicate which will eventually lead your software to throw exceptions regarding that.
Make a Generic List of custom class data. Something like this:
public class UserPoint
{
public string Name { get; set; }
public int Points { get; set; }
}
Then like below you can read your text file and load the data into the Generic List.
public static void Main(string[] args)
{
var lines = System.IO.File.ReadAllLines(#"myfile.txt"); // I assume each user info is separated by a newline.
var pointsList = new List<UserPoint>();
foreach(var line in lines)
{
var splittedLine = line.Split(';');
if(splittedLine.Length < 1) continue;
var userPoint = new UserPoint { Name = splittedLine[0].Trim() };
if(splittedLine.Length > 1 && int.TryParse(splittedLine[1].Trim(), out var points)) // updated the direct conversion to safe conversion with tryparse.
{
userPoint.Points = points; // To get rid of extra parsing you can also keep Points property as string.
}
pointsList.Add(userPoint);
}
}
PS: There are multiple other ways to read the text file but I used a simple way, You can also use FileStream and StreamReader classes if you like. Read more on reading text files on these MSDN Articles.
EDIT:
I notice you want to iterate and print through the list as well, you can do in the first loop or you can write a loop again. See:
foreach(var userPoint in pointsList)
{
Console.WriteLine($"Name: {userPoint.Name}, Points: {userPoint.Points}");
}
EDIT 2:
OP has asked to sort the list in descending order based on the points value. Here is a solution for that:
pointsList = pointsList.OrderByDescending(userPoint => userPoint.Points).ToList();

How To Translate Unicode Value To Emoji String in C#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to convert emoji's like the one below to c# code so I can put the code in a TreeView node or facebook or other social engine. I tried the code for airplane and shows a little airplane in the treenode. But I use another airplace code like U+1F6E9 it just shows a little rectangle not the emoji. Please help.
string tnt = "Airplane " + char.ConvertFromUtf32(int.Parse("U+2708".Substring(2), System.Globalization.NumberStyles.HexNumber));
MyTreeView.Nodes.Add(tnt);
Here is a class you may use to implement different emoji's in your application.
public class Emoji
{
readonly int[] codes;
public Emoji(int[] codes)
{
this.codes = codes;
}
public Emoji(int code)
{
codes = new int[] { code };
}
public override string ToString()
{
if (codes == null)
return string.Empty;
var sb = new StringBuilder(codes.Length);
foreach (var code in codes)
sb.Append(Char.ConvertFromUtf32(code));
return sb.ToString();
}
}
I would use the codes from this site unicode.org
Instead of using the code it has on the site which is something like 'U+1F366' I would use '0x1F366' to specify hexadecimal notation. Hope this helps.

How To Create Emoji Strings From Unicode Value? [duplicate]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I'm trying to convert emoji's like the one below to c# code so I can put the code in a TreeView node or facebook or other social engine. I tried the code for airplane and shows a little airplane in the treenode. But I use another airplace code like U+1F6E9 it just shows a little rectangle not the emoji. Please help.
string tnt = "Airplane " + char.ConvertFromUtf32(int.Parse("U+2708".Substring(2), System.Globalization.NumberStyles.HexNumber));
MyTreeView.Nodes.Add(tnt);
Here is a class you may use to implement different emoji's in your application.
public class Emoji
{
readonly int[] codes;
public Emoji(int[] codes)
{
this.codes = codes;
}
public Emoji(int code)
{
codes = new int[] { code };
}
public override string ToString()
{
if (codes == null)
return string.Empty;
var sb = new StringBuilder(codes.Length);
foreach (var code in codes)
sb.Append(Char.ConvertFromUtf32(code));
return sb.ToString();
}
}
I would use the codes from this site unicode.org
Instead of using the code it has on the site which is something like 'U+1F366' I would use '0x1F366' to specify hexadecimal notation. Hope this helps.

C# Changing the method I call based on a string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
First of all sorry if this is a duplicate. I dont exactly know how to search for this.
I have a question about how to be able to use a saved string to change what type of method I call
MenuBar.Dock = Dockstyle.DockStyleString //DockStyleString is a string defined somewhere with either Top or Bottom
So, according to your example you seem to be using an enumerator. Enum has utilities that would 'convert' a string into the right enum value. Also you can have an utility class that does that for you.
DockstyleUtils.FromString("DockStyleString");
This would return an enum Dockstyle.DockstyleString.
So, you can use it MenuBar.Dock = DockstyleUtils.FromString("DockStyleString");
I created this method you can use...
public DockStyle ConvertDockingStyleFromString(string dockingStyle)
{
return (DockStyle)Enum.Parse(typeof(DockStyle), dockingStyle);
}
There you go.
Some of this depends on what you want to do with the string once you have it. You can use the code in #PepitoFernandez's answer to convert it to an enum. If you'd like to then use it to determine what method to call against an object, you have a few options.
The first is that if it's a known set of strings, you could use a switch statement:
switch (stringVariable) {
case "stringA": methodA(); break;
case "stringB": methodB(); break;
...
// If you get a "bad" string, you WANT to throw an exception to make
// debugging easier
default: throw new ArgumentException("Method name not recognized");
}
Obviously, you can also replace this with enum values if you do the conversion first. (That's actually not a bad idea because if you get a "bad" string you
The other option (if you want to do it dynamically at runtime) is to do the call using reflection, like this:
public class SomeClass
{
public void MethodA()
{
Console.WriteLine("MethodA");
}
}
static void Main(string[] args)
{
Type type = typeof(SomeClass);
// Obviously, you'll want to replace the hardcode with your string
MethodInfo method = type.GetMethod("MethodA");
SomeClass cls = new SomeClass();
// The second argument is the parameters; I pass null here because
// there aren't any in this case
method.Invoke(cls, null);
}

Call two different function based on some value using delegate [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have two function with different functionality and i want to call them based on some value. without if else or switch case block. like i have
Dictionary<string, string> intentMap = new Dictionary<string, string>();
intentMap.Add("rootIntent", "TicketbookingInformation");
intentMap.Add("rootIntent", "OrderInformation");
and i have two function
public bool BookTicket()
{
// to do
}
public bool BookOrder()
{
// to do
}
I want to switch these function if intentMap has TicketbookingInformation then call BookTicket method or if intentMap has OrderInformation then call BookOrder method.
I want to do in generic way using deleagtes so in future if I have some more use case (new method), so i can utilize the same functionlaity without modifying much.
Use another dictionary, this time of type <string, Func<bool>:
var delegateMap = new Dictionary<string, Func<bool>>()
{
{ "TicketBookingInformation", BookTicket },
{ "OrderInformation", BookOrder }
};
foreach (var intent in intentMap)
{
bool result = delegateMap[intent.Value]();
}
I won't answer to your question directly. #Abion47 has answered perfectly fine, i would answer the same.
But i think having having two dictionaries one having the token, and another having the delegate is more obscure then having single method for dispatching the result.
Here is my point with a bit code.
This the dictionary version.
var intentMap = new Dicionary<string, string>();
var delegateMap = new Dictionary<string, Func<bool>>();
delegate.Map.Add("TicketbookingInformation", BookTicket);
delegateMap.Add("OrderInformation", BookOrder);
Then the usage will be something like this
var token = ...;
if(intentMap.ContainsKey(token))
{
var delegateToken = intenMap[token];
if(delegateMap.ContainsKey(delegateToken))
{
var delegatedMethod = delegateMap(delegateToken);
return delegatedMethod();
}
}
So adding new token and delegate will means extending the dictionaries and i can't see how this is different then extending a switch statement.
Second version which i think will be generic enough and will need not too much modification where used will be something like this.
First i will suggest having the "TicketbookingInformation" and "OrderInformation" as enumeration type, this way having a switch statement will benefit from the compiler helping when you have some missing case to handle. This of course is applicable if you know upfront all the possible values ;]
bool PredicateDispatch(DelegateToken token)
{
switch(token)
{
DelegateToken.TicketbookingInformation: return BookTicket();
DelegateToken.OrderInformation: return BookOrder()
}
}
this way you will need to extend this single method to handle new tokens. It is more readable approach and you don't lose anything compared with the dictionary version. So i will do it this way. If don't have of course more complicated code and your example is more more simplified ;]

Categories

Resources