I am new to using Selenium with C#, so this might be too easy for most of you. Also, I did search on this forum and couldn't get a satisfactory response, so will go ahead and ask the problem I am trying to solve.
I want to use Assert between two different elements to check for a value. If first element finds it I want to exit the loop and pass the test, or else the second element; otherwise fail the test if not found in both.
p0 is the string that I am passing in my test and I want to check that string is there either in tranName or accName
transName is int and accName is string. But I am assigning both to same var.
Assert.That(transName.Text.Equals(p0));
Assert.That(accName.Text.Equals(p0));
Can you please suggest, how best to deal with this.
Thanks..!!
transName = driver.FindElement(By.xxxx);
if(transName.Displayed) {
Assert.That(transName.Text.Equals(p0));
}
else {
accName= driver.FindElement(By.xxxx);
if(accName.Displayed) {
Assert.That(accName.Text.Equals(p0));
}
else {
Assert.That(True.Equals(False))
}
}
You don't need to do all those conditionals. Here's an easier/shorter way to do this using LINQ, NUnit, and a custom CSS selector locator.
List<string> text = Driver.FindElements(By.CssSelector("#XXX, #YYY")).Where(e => e.Displayed).Select(e => e.Text).ToList();
CollectionAssert.Contains(text, p0);
Basically we combine the two locators into one where #XXX means an id of XXX and the comma is an OR operator. Selenium finds all the elements that match those two locators and then filters those down to those that are visible. Then it get the .Text of each of the visible elements and returns them as a List. The second line is NUnit where you can assert a string in a List<string>.
int n;
bool searchWithNumber = int.TryParse(p0, out n);
var transName = driver.FindElement(By.Id("XXX"));
if (transName.Displayed && !searchWithNumber)
{
Assert.That(tarnsName.Text.Contains(p0));
}
else if (searchWithNumber)
{
var accName = driver.FindElement(By.Id("YYY"));
if (accName.Displayed)
{
Assert.That(accName.Text.Contains(p0));
}
else
{
Assert.That(true.Equals(false));
}
}
So what I am doing above is. I am first parsing the string p0 to extract number if it has, which I am then assigning to bool. Then in the loop I am first checking if the tranName has any number then skip to else if condition, if it has text then execute the first condition.
Related
I am implementing a simple profanity filter for my player username in unity. But for some reason, I am not getting the correct result. As shown in code below, I am extracting the words from a TextAsset .txt file into a list of strings and looping through the list to check if the username contains a badword using Contains() function. But the Contains() function is always returning false.
What else I tried :
Tried "==" operator to test for exact string, but still returning false
Tried Equals() function to test for exact string, but still returning false
Tried regex but still not working
Note : both checkText and badWordToUpper are of type string and are showing right results. But when compared, not working.
Sound like a white char is hidden in a bad word. If you are on Windows, the break line is "\r\n". You can check with :
void Start()
{
strBlockList = ...;
var wordWithWhiteChar = strBlockList.Where(w => w.Any(c => char.IsWhiteSpace(c))).ToList();
// Check wordWithWhiteChar
}
If it's the case, you need to clean each word like :
void Start()
{
strBlockList = ...;
for (int i = 0; i < strBlockList.Length ; i++)
{
strBlockList[i] = Regex.Replace(strBlockList[i], #"\s+", string.Empty).ToUpper();
}
}
Bonus : To upper case in Start to cache the operation.
I am aware this question as been asked. And I am not really looking for a function to do so. I was hoping to get some tips on making a little method I made better. Basically, take a long string, and search for a smaller string inside of it. I am aware that there is literally always a million ways to do things better, and that is what brought me here.
Please take a look at the code snippet, and let me know what you think. No, its not very complex, yes it does work for my needs, but I am more interested in learning where the pain points would be using this for something I would assume it would work for, but would not for such and such reason. I hope that makes sense. But to give this question a way to be answered for SO, is this a strong way to perform this task (I somewhat know the answer :) )
Super interested in constructive criticism, not just in "that's bad". I implore you do elaborate on such a thought so I can get the most out of the responses.
public static Boolean FindTextInString(string strTextToSearch, string strTextToLookFor)
{
//put the string to search into lower case
string strTextToSearchLower = strTextToSearch.ToLower();
//put the text to look for to lower case
string strTextToLookForLower = strTextToLookFor.ToLower();
//get the length of both of the strings
int intTextToLookForLength = strTextToLookForLower.Length;
int intTextToSearch = strTextToSearchLower.Length;
//loop through the division amount so we can check each part of the search text
for(int i = 0; i < intTextToSearch; i++)
{
//substring at multiple positions and see if it can be found
if (strTextToSearchLower.Substring(i,intTextToLookForLength) == strTextToLookForLower)
{
//return true if we found a matching string within the search in text
return true;
}
}
//otherwise we will return false
return false;
}
If you only care about finding a substring inside a string, just use String.Contains()
Example:
string string_to_search = "the cat jumped onto the table";
string string_to_find = "jumped onto";
return string_to_search.ToLower().Contains(string_to_find.ToLower());
You can reuse VB's Like operator this way:
1) Make a reference to Microsoft.VisualBasic.dll library.
2) Use the following code.
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
if (LikeOperator.LikeString(Source: "11", Pattern: "11*", CompareOption: CompareMethod.Text)
{
// Your code here...
}
To implement your function in a case-insensitive way, it may be more appropriate to use IndexOf instead of the combination of two ToLower() calls with Contains. This is both because ToLower() will generate a new string, and because of the Turkish İ Problem.
Something like the following should do the trick, where it returns False if either term is null, otherwise uses a case-insensitive IndexOf call to determine if the search term exists in the source string:
public static bool SourceContainsSearch(string source, string search)
{
return search != null &&
source?.IndexOf(search, StringComparison.OrdinalIgnoreCase) > -1;
}
I want to compare two integer arrays and then print the equals out. I tried the Intersect method:
var checkingDuplicates = boughttickets.Intersect(winningtickets).Any();
and then used a if-statement:
if (checkingDuplicates == false)
{
Console.WriteLine("Sorry, You didn't win anything");
}
else
{
Console.WriteLine(checkingDuplicates);
}
However, the output always returns true with this if-statement.
I am truly sorry if i have some Spelling misstakes, my native language is not English.
Any takes a sequence of items and returns a boolean indicating if there are any items in that sequence.
You don't want to know if there are any items in that sequence, you just want to know what all of the items in that sequence are. That means don't call Any, and you'll have your sequence of the intersection of those two arrays.
First i would say read the documentation. From your comment above, you can not call .toString() on IEnumerable withou defining .tostring implementation.
Try this
var checkingDuplicates = boughttickets.Intersect(winningtickets);
if (!checkingDuplicates.Any())
{
Console.WriteLine("Sorry, You didn't win anything");
}
else
{
foreach(TICKET checkingDuplicate in checkingDuplicates)
{
Console.WriteLine("FETCH AND PRINT YOUR TICKET INFORMATION FROM TICKET OBJECT/CLASS");
}
}
In the foreach loop above TICKET is the type of object you are using on the IEnumerable lists above.
I have a program that needs to compare any given string with a predefined string and determine if an insertion error, deletion error, transposition or substitution error was made.
For example, if the word dog was presented to the user and the user submits dogs or doge, it should notify the user that an insertion error has been made.
How do I go about this?
You probably need to write a method for each of the individual error types to see if it's an error, like:
bool IsInsertionError(string expected, string actual) {
// Maybe look at all of the chars in expected, to see if all of them
// are there in actual, in the correct order, but there's an additional one
}
bool IsDeletionError(string expected, string actual) {
// Do the reverse of IsInsertionError - see if all the letters
// of actual are in expected, in the correct order,
// but there's an additional one
}
bool IsTransposition(string expected, string actual) {
// This one might be a little tricker - maybe loop through all the chars,
// and if expected[i] != actual[i], check to see if
// expected[i+1] = actual[i] and actual[i-1]=expected[i]
// or something like that
}
Once you build out all the individual rules, and you first check for regular equality, fire each of them off one at a time.
You've got to just break problems like this down into small components, then once you have a bunch of easy problems, solve them one at a time.
Off the top of my head but I think this should get you started:
Insertion and Deletion should be pretty simple; just check the lengths of the strings.
if(originalString.Length > newString.Length)
{
//deletion
}
else if(originalString.Length < newString.Length)
{
//insertion
}
To detect transposition, check if the lengths match and if so, you could create two List<char> from the two strings. Then check if they match using the expression below
bool isTransposed = originalList.OrderBy(x => x).SequenceEquals(newList.OrderBy(y => y));
To detect substitution, you could use the Hamming Distance and check if it's greater than 0.
I would suggest you to create a function which will take a parameter as the input sting. The function would look more or less like this. Use the function wherever you want then.
private void CheckString(string userString)
{
string predefinedString = "dog";
if (userString == predefinedString)
{
// write your logic here
}
else
{
MessageBox.Show("Incorrect word"); // notify the users about incorrect word over here
}
}
I have a sorted StringList and wanted to replace
foreach (string line3 in CardBase.cardList)
if (line3.ToLower().IndexOf((cardName + Config.EditionShortToLong(edition)).ToLower()) >= 0)
{
return true;
}
with a binarySearch, since the cardList ist rather large(~18k) and this search takes up around 80% of the time.
So I found the List.BinarySearch-Methode, but my problem is that the lines in the cardList look like this:
Brindle_Boar_(Magic_2012).c1p247924.prod
But I have no way to generate the c1p... , which is a problem cause the List.BinarySearch only finds exact matches.
How do I modify List.BinarySearch so that it finds a match if only a part of the string matches?
e. g.
searching for Brindle_Boar_(Magic_2012) should return the position of Brindle_Boar_(Magic_2012).c1p247924.prod
List.BinarySearch will return the ones complement of the index of the next item larger than the request if an exact match is not found.
So, you can do it like this (assuming you'll never get an exact match):
var key = (cardName + Config.EditionShortToLong(edition)).ToLower();
var list = CardBase.cardList;
var index = ~list.BinarySearch(key);
return index != list.Count && list[index].StartsWith(key);
BinarySearch() has an overload that takes an IComparer<T> has second parameter, implement a custom comparer and return 0 when you have a match within the string - you can use the same IndexOf() method there.
Edit:
Does a binary search make sense in your scenario? How do you determine that a certain item is "less" or "greater" than another item? Right now you only provide what would constitute a match. Only if you can answer this question, binary search applies in the first place.
You can take a look at the C5 Generic Collection Library (you can install it via NuGet also).
Use the SortedArray(T) type for your collection. It provides a handful of methods that could prove useful. You can even query for ranges of items very efficiently.
var data = new SortedArray<string>();
// query for first string greater than "Brindle_Boar_(Magic_2012)" an check if it starts
// with "Brindle_Boar_(Magic_2012)"
var a = data.RangeFrom("Brindle_Boar_(Magic_2012)").FirstOrDefault();
return a.StartsWith("Brindle_Boar_(Magic_2012)");
// query for first 5 items that start with "Brindle_Boar"
var b = data.RangeFrom("string").Take(5).Where(s => s.StartsWith("Brindle_Boar"));
// query for all items that start with "Brindle_Boar" (provided only ascii chars)
var c = data.RangeFromTo("Brindle_Boar", "Brindle_Boar~").ToList()
// query for all items that start with "Brindle_Boar", iterates until first non-match
var d = data.RangeFrom("Brindle_Boar").TakeWhile(s => s.StartsWith("Brindle_Boar"));
The RageFrom... methods perform a binary search, find the first element greater than or equal to your argument, that returns an iterator from that position