Code optimization in selenium webdriver for driver.findElement - selenium-webdriver

The code which i have written uses find Element method more than 32 times:
i wanted to create a common method for find Element
should i declare any generic method ?

A bit more of info on your code will help answer this. If you are trying to access different elements on your page then you would directly or indirectly end up making these 32 calls.
So first check if you need 32 different elements or not. If not, consider storing the results in variables and reusing them (again depends on your code/flows).

Although it won't make any difference in doing it, but if still you want you can make a method somewhat like this:
public WebElement find(String type,String locator){
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement we;
if(type.equalsIgnoreCase("xpath")){
we = driver.findElement(By.xpath(locator));
}
if(type.equalsIgnoreCase("id")){
we = driver.findElement(By.id(locator));
}
// and so on...
}
And you can use it like this:
WebElement newButton1 = find("id","button1");
WebElement newLink1 = find("xpath","//a[text()='xyz']");

Related

Matching elements by property ends with in WebDriverIO

I am used to Selenium WebDriver were I can do something like this:
ReadOnlyCollection<IWebElement> magicPills = _webDriver.FindElements(By.CssSelector("span[id$='_Blue_Pills']"));
How do I do the same thing in WebDriverIO? I couldn't find anything in the docs that stated StartsWith, EndsWith, or whatever.
My first failed attempt is:
const magicPills = $('span.$_Blue_Pills');
Give a try as like below in wdio:
const magicPills = $$('span[id$='_Blue_Pills']');
$() returns a webElement not elements
and you can use the same cssSelector you tried in selenium_webdriver(because wdio will automatically resolves to cssSelector internally).
Please try the above and see if it works.

How to find which element failed comparison between arrays in Kotlin?

I'm writing automated tests for one site. There's a page with all items added to the cart. Maximum items is 58. Instead of verification of each element one by one I decided to create 2 arrays filled with strings: 1 with correct names : String and 1 with names : String I got from the site. Then I compare those 2 arrays with contentEquals.
If that comparison fails, how do I know which element exactly caused comparison fail?
Short simple of what I have now:
#Test
fun verifyNamesOfAddedItems () {
val getAllElementsNames = arrayOf(materials.text, element2.text,
element3.text...)
val correctElementsNames = arrayOf("name1", "name2", "name3"...)
val areArraysEqual = getAllElementsNames contentEquals correctElementsNames
if (!areArraysEqual) {
assert(false)
} else {
assert(true)
}
}
This test fails if 2 arrays are not the same but it doesn't show me the details, so is there a way to see more details of fail, e.g. element that failed comparison?
Thanks.
I recommend using a matcher library like Hamcrest or AssertJ in tests. They provide much better error messages for cases like this. In this case with Hamcrest it would be:
import org.hamcrest.Matchers.*
assertThat(getAllElementsNames, contains(*correctElementsNames))
// or just
assertThat(getAllElementsNames, contains("name1", "name2", "name3", ...))
There are also matcher libraries made specifically for Kotlin: https://github.com/kotlintest/kotlintest, https://yobriefca.se/expect.kt/, https://github.com/winterbe/expekt, https://github.com/MarkusAmshove/Kluent, probably more. Tests using them should be even more readable, but I haven't tried any of them. Look at their documentation and examples and pick the one you like.
You need to find the intersection between the two collections. Intersection will be the common elements. After than removing the intersection collection from the collection you want to perform the test will give you the complementary elements.
val intersection = getAllElementsNames.intersect(correctElementsNames)
getAllElementsNames.removeAll(intersection)

Interception not working as expected with Entity Framework 6

I'm trying to follow the interception example shown here to get it working with EF 6 but running into a problem with the function RewriteFullTextQuery as shown in Figure 1. The interception seems to work but it does not actually execute the logic in the for loop of the RewriteFullTextQuery method because the cmd.Parameters.Count is always zero. Furthermore the cmd.CommandText property seems to be displaying the correct SQL query which I take as another piece of evidence that the interception is working correctly.
Figure 1: RewriteFullTextQuery Code Excerpt
public static void RewriteFullTextQuery(DbCommand cmd)
{
string text = cmd.CommandText;
for (int i = 0; i < cmd.Parameters.Count; i++)
{
DbParameter parameter = cmd.Parameters[i];
if (parameter.DbType.In(DbType.String, DbType.AnsiString, DbType.StringFixedLength, DbType.AnsiStringFixedLength))
{
The RewriteFullTextQuery function is being called by the ReaderExecuting function shown in Figure 2 which gives it the command argument that is causing all the trouble.
Figure 2: ReaderExecuting Function
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
RewriteFullTextQuery(command);
}
Even though my code isn't exactly the same as the example, the interception seems to be working so it is making me wonder what conditions is it that will populate the command to have a Parameters.Count of more than zero?
It works only if you pass the parameter to the query as a variable. If you use a literal EF won't use parameters.
I mean, this won't generate any parameter
context.Notes.Where(_ => _.NoteText == "CompareValue").Count();
This will
string compareValue = "CompareValue";
context.Notes.Where(_ => _.NoteText == compareValue).Count();
It turns out that it is because of the way Entity Framework generates the SQL. If you pass in a string literal as your search value to your LINQ statement it does not generate a SQL that makes use of a parameter. But if you pass in your search value as a variable, it will generate the SQL that utilizes a parameter. A solution (for dynamic queries) and more details can be found on this blog.

sendKeys(Keys.TAB) not working in JMeter Webdriver Sampler

I am trying to enter a value into a textfield then Tab to the next field (which also enters the value). The Keys.TAB method does not seem to be working.
My code is as follows:
var Keys = JavaImporter(org.openqa.selenium.Keys)
var input = WDS.browser.findElement(pkg.By.xpath('xpath_to_input'))
input.sendKeys('value')
input.sendKeys(Keys.TAB)
I am getting the following error:
sun.org.mozilla.javascript.internal.EvaluatorException: Can't find method org.openqa.selenium.remote.RemoteWebElement.sendKeys(string). <Unknown source>
Thank you for your help. I have tried all sorts of things and it will not work.
In addition to what ekuusela suggests there are 2 more options:
Use \t escape sequence like:
input.sendKeys('value\t');
Use java.awt.Robot approach as follows:
input.sendKeys('value')
var robot = new java.awt.Robot()
var keyEvent = java.awt.event.KeyEvent
robot.keyPress(keyEvent.VK_TAB)
robot.keyRelease(keyEvent.VK_TAB)
Remember that "Robot" approach simulates native key and mouse event on the machine where it is executed so if you use remote webdriver instance it won't play.
For more WebDriver Sampler tips and tricks see The WebDriver Sampler: Your Top 10 Questions Answered guide.
If you use Java 6 you must pass the string in an array, like this:
var input = WDS.browser.findElement(pkg.By.xpath('xpath_to_input'))
input.sendKeys(['value'])
input.sendKeys([Keys.TAB])
http://jmeter-plugins.org/wiki/WebDriverSampler/

AS3 Copy multidimensional arrays/Vector

I have two Vectors one called "SET_grid" which should never me changed and one called "tmp_grid" which can, but how do i copy SET_grid to tmp_grid without binding it to the original, so if tmp_grid change then the SET_gird doesn't, these Vectors are both multidimensional e.g.
public var tmp_grid:Vector.<Vector.<node>> = new Vector.<Vector.<node>>(2);
public var SET_grid:Vector.<Vector.<node>> = new Vector.<Vector.<node>>(2);
so i would use them like this....
tmp_grid[x][y].sayhello();
tmp_grid = SET_grid does not work
tmp_grid = SET_grid.concat(); // nor does this one
Any help would be great
Nested arrays cannot be cloned without iterations. It's because they're nested :)
What this means is that you have to use nested loops and push to second vectors..

Resources