Selenium Extract the ID of an element found via text in Java - java

I have a set of Cucumber tests I wish to execute using selenium and Chrome. Most of my test work fine. I am currently trying to parametertize one particular step so that anyone using the Test can simply name the elements they want and the test will find them.
The Cucumber test looks something like this:
When I go to the "Inventory" / "Inventory" application
And I search the "Description" field for "a"
I was able to get the when statement to parameterized very easily, but the And Statement is giving me a bit more trouble. The page itself has several sets of tables with a consistent id scheme The xpath for the header with the word "Description" looks like this:
//label[#id='m6a7dfd2f_ttrow_[C:2]_ttitle-lb']
and the input field has this xpath
//td[#id='m6a7dfd2f_tfrow_[C:2]-c']/input
For reference the immediate next input field, located below the label room, has this xpath
//td[#id='m6a7dfd2f_tfrow_[C:3]-c']/input
So each label and input field have the same id scheme and only the number following "c:" seems to change. That number is the same for both the label and it's corresponding input value. I would like to some how scan the page for the word "Description" and extract the ID of the element associated with it and then truncate it down to the number. I already have variables set up to hold the rest and it would hopefully as follows:
#When("^I search the \"([^\"]*)\" field for \"([^\"]*)\"$")
public void i_search_the_field_for(String searchField, String searchItem) throws Throwable {
String baseinputXPath = "//td[#id='m6a7dfd2f_tfrow_[c:";
String endinputXPath = "]-c']/input";
String elementNumber = "The return of some sort of method based on searchField";//TODO
driver.findElement(By.xpath(baseinputXPath + elementNumber + endinputXPath)).clear();
driver.findElement(By.xpath(baseinputXPath + elementNumber + endinputXPath)).sendKeys(searchItem);
driver.findElement(By.id("search_button_img")).click();
If there is a way to pull out that number based only on the description text, that would be super helpful.

Use a xpath to search for the WebElement with word 'Description' in a label tag.
"//label[text(),'Description']" or "//label[.='Description']" or "//label[contains(text(),'Description')]"
Use the getAttribute("id") to get the id.

Related

how to use Aspose to conditionally show a table and hide another table in word template?

I have a word template with two table, I want to use Aspose to show a table and hide another table in word template based on a variable in java code, how to achieve this?
You can easily achieve what you need using IF field in your MS Word template document.
https://support.microsoft.com/en-us/office/field-codes-if-field-9f79e82f-e53b-4ff5-9d2c-ae3b22b7eb5e
in the condition you can insert a merge field or a bookmark and then update the condition upon execution mail merge or setting bookmark value using Aspose.Words.
For example, see the screenshot of the template document and the code to execute mail merge using Aspose.Words.
Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().execute(new String[] { "test" }, new String[] { "first" });
doc.save("C:\\Temp\\out.docx");
If the output format is supposed to be MS Word document, you can also call Document.unlinkFields() method before saving, in this case IF field will be removed from the document and only the result will be preserved.

DCM4CHE retrieve value based on tag in a sequence

Using DCM4CHE to retrieve values based on the tag name in plain xml is pretty straightforward.
For example if I want to retrieve the value of the attribute AccessionNumber:
String accessiongNumber = attribute.getString(Tag.AccessionNumber);
But what is the best approach when dealing with Sequence? I want to retrieve a value using its Tag name, but the value is 5 layers deep inside a Sequence.
In this case, I can get to the Sequence I want with:
Sequence recordSequence = attribute.getSequence(Tag.RecordSequence);
Is there a way to retrieve a value by its tag directly once I have the sequence that the value is embedded in?
Try using the Attributes.getNestedDataset methods. These will get you the attributes in the sequence. Something like:
Attributes refStudy = attribute.getNestedDataset(Tag.ReferencedStudySeequence, 0);
String refSopiuid = refStudy.getString(Tag.ReferencedSOPInstanceUID);

MongoDB full text search index: error: too many text index for, why?

I have one problem, I have collection and I want to set text search index to 2 fields(description and title). But when I add second index I get following error and text search stopped working.
{ "serverUsed" : "localhost/127.0.0.1:27017" , "ok" : 0.0 , "errmsg" : "too many text index for: testdb.users"}
when I delete one index search start work again. what is the problem? One collections support full text search index only for one field????
I am using the current version of mongodb under windows and I am using mongodb java driver API.
Thanks
MongoDB only allows one text-index per collection.
But you can use a text-index which spans multiple fields:
db.collection.ensureIndex( {
description: "text",
title: "text"
} );
That way you will get results when the phrase you are searching for is found in either. When this is not what you want, like when you have two search-queries which each return results from one of the fields but not the other, you have two options.
use a multi-field text index, but discard the results which come from the wrong field on the application layer.
extract one of the two fields to a different collection. The documents in that collection could either contain full copies, redacted copies or just the field you index and the _id of the original document.
To create a text based index on a key, use command db.collectionName.ensureIndex({'textColumnName': 'text'}). After this index is applied, use the search commands to search for a word i.e. db.collectionName.find({$text: {$search:'your text here'}}). There is a text score based on which the results are ranked, to see it project it in the score key like this : db.collectionName.find({$text: {$search:'your text here'}}, {score: {$meta: 'textScore'}}).sort({score: {$meta: 'textScore'}}).
If we create a text index on the title field of the movies collection, and then perform the text search db.movies.find( { $text : { $search : "Big Lebowski" } } ). The following documents will be returned, assuming they are in the movies collection:
{ "title" : "The Big Lebowski" , star: "Jeff Bridges" }
{ "title" : "Big" , star : "Tom Hanks" }
{ "title" : "Big Fish" , star: "Ewan McGregor" }
This is because, there will be a ***logical OR***ing on Big & Lebowski.

Elasticsearch multiple fields autosuggestion

I want to implement autosuggestion functionality using elastic search. I can use nGram filters to match partial words on multiple fields and its working fine as expected. Output of the search returns full document with multiple fields as required. Now my problem is, how do I give autosuggestion to the user based on the matching field. e.g. I have got 5 fields:
{userId:'rakesh',firstName:'Rakesh','lastName':'Goyal','mobileNo':'123-123-1234','alternativeMobileNo':'123-123-1235'}
{userId:'goyal',firstName:'Goyal','lastName':'Rakshit','mobileNo':'123-123-1236','alternativeMobileNo':'123-123-1237'}
In the above example if user types 123, I want to return 123-123-1234, 123-123-1235, 123-123-1236, 123-123-1237 (4 auto suggestions).
Similarly if user types Rak, I want to return Rakesh, Rakshit (2 auto suggestions).
How do I know match exists in mobileNo and alternativeMobileNo field for first example and return results accordingly?
How do I know match exists in firstName and lastName field for second example and return results accordingly?
How do I give autosuggestion to the user based on the matching field?
When user types 123, store it in a Java variable, prepare a query like below inserting that variable into and send a request to ElasticSearch.
{
"query" : {
"query_string" : {
"query" : "*123*"
}
}
}
The above query will manage to check it in both fields mobileNo and alternativeMobileNo.
Similarly, if user types Rak, the query will be similar to the previous one,
{
"query" : {
"query_string" : {
"query" : "*Rak*"
}
}
}
And I think you want to use highlighter api to answer your last how questions, which allows to highlight search results on one or more fields.
A screenshot of highlight example in es :

How to distinguish between table elements with identical names? Selenium Java

I need to get to the second table's element. However since the web page contains two tables, I always end up getting an element from the first table.
Here is the code:
driver.get("http:.............");
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
String el = driver.findElement(By.xpath("//tr[1]/td[2]")).getText();
System.out.println(el);
//I need to get the following value: $0.00564000. How can I overcome this issue?
Try this:
String el = driver.findElements(By.xpath("//tr[2]/td[2]")).get(1).getText();

Categories

Resources