StaleElementReference Exception - selenium-webdriver

I am struggling with the StaleElementReferenceException problem. I have seen quite a number of discussions on the topic but I haven't been able to find a solution to the issue that I am facing.
The test is to get all the links on the footer of the web page and then verify whether the link is active or not by clicking on it and verifying the title of the page.
First of all, I find & store all the links in an array list. I compare the link name with the values retrieved from the database. Then for each link, I click on it and verify the page title. Then using 'driver.navigate.back()', go back to the original page and continue with the rest of the links.
However, when the control returns back to the page, the StaleElementReferenceException occurs.
Can anyone suggest me a way out of this?
Thanks,
Anuj

When you are storing all the links in the footer you are grabbing those elements as they are at that point in time. Upon navigating to a different page those particular elements no longer exist. When you return to the back these elements have been created anew.
While the elements are the same via identifiers, they are different instances and thus your old elements in your array are 'stale'.
I would suggest storing only the link identifiers (not the link elements themselves) as strings and then search for them each time the page has loaded.

I faced a similar issue, in my case when I type something into a text box it would navigate to another page, so while I come back on the previous page, that object gets stale.
So this was causing the exception, I handled it by again initialising the elements as below -
PageFactory.initElements(driver, Test.class);
So when you navigate back, make sure you are initialising all the elements of that page again, so that the object does not get stale.

You can handle going and coming to new tab as follows:
String baseHandle = driver.getWindowHandle();
Set<String> sr = driver.getWindowHandles();
if (sr.size()>1){
Set<String> sr1 = driver.getWindowHandles();
sr1.remove(baseHandle);
Iterator itr = sr1.iterator();
driver.switchTo().window(itr.next().toString());
System.out.println("Page Title is : " + driver.getTitle());
driver.close();
driver.switchTo().window(baseHandle);

Related

How to disable index searching on the list and just get the articles indexed on search results?

Using 2sxc Blogg App and when using search I get the results of the blog home page listed, which simply list the blog home and the article titles which all take the user to the blog home page, so they are pretty much useless links, then I get the actual articles with the links to the articles. So I need to suppress the blog page itself, but not its dynamic children (the articles).
/help <-- no, thanks, your links are useless.
/help/post <-- yes, please, list all.
Any idea on how I could achieve that? I got directed to CustomizeData() doc, but I have no idea what to do. The current one set on the main blog list page is as follows:
#functions{
/// <summary>
/// Populate the search - ensure that each entity has an own url/page
/// </summary>
/// <param name="searchInfos"></param>
/// <param name="moduleInfo"></param>
/// <param name="startDate"></param>
public override void CustomizeSearch(Dictionary<string, List<ToSic.SexyContent.Search.ISearchInfo>> searchInfos, DotNetNuke.Entities.Modules.ModuleInfo moduleInfo, DateTime startDate)
{
foreach (var si in searchInfos["SearchIndex"])
{
si.QueryString = "post=" + AsDynamic(si.Entity).UrlKey;
}
}
}
welcome to StackOverflow ;)
The basic DNN index asks each module for the data it has, and then builds the index on that. Since a module can have multiple items for the search, they are each an own "document" which can be configured - for example what URL to use in the search results. To enable view-developers to customize these things, 2sxc has this hook to customize the search results. So the way it's meant to work is...
the backend collects the data
then detects that the search index is being built (and not a user viewing the page)
then call the code for optional reconfiguration
then pass the items on to DNN search
So what the code should do is take each item as it was prepared by the backend, change the url to use and then let the rest of the system do its magic. If this isn't working, there are a few possibilities:
something in DNN or 2sxc is broken (I really hope that's not it)
the code caused errors and since it happens in the background, you don't see it
the data is not being passed to the code, for example because it was filtered out - for example, old data isn't updated in the index, because the indexer will ask for new data only, and therefor older data won't be updated on normal re-indexes, no matter how you update the code.
Let's try to find out what the cause is
open the app query https://azing.org/2sxc/r/T1GdqnNa and select the **Blog Posts for Home and Tags", and test-run the query to see if it gives you results. if not, something may be wrong with the query. In the json-looking test-results on the screen, do check if there is something in the set "SearchIndex" - this is the data stream that skips paging and returns all items. If this is empty, get back to us. Note: if you don't get any results, do check what Test-Parameters the query is using (box to the right), and maybe edit the ModuleId in case it's wrong
check if you see any events in the DNN event-log. if you don't, make sure you re-index the whole data in DNN and check again.
Post your results, so we can check how this can be fixed ;)

How to have single store for each tab in redux [reactjs]?

We have a web app composed of several pages containing links of data retrieved from database. Upon clicking one link[data], the user should be directed to another page. For navigation between pages, we have used breadcrumb. The breadcrumb is stored in redux store. Currently, when the user tries to ctrl+click or open link in new tab, we managed to use single store across multiple tabs. Hence, if the user opens 3 separate links [data] in new tabs, the updates made on the breadcrumb affect previously opened tabs when these tabs/pages are refreshed. For example:
In homepage, I have these links:
Data_1
Data_2
Data_3
Current breadcrumb in the homepage is like this:
HomePage/
Once the user opens Data_1 in new tab, the expected breadcrumb in the new tab is:
HomePage/Data_1/
Similarly, if the user tries to open Data_2 and Data_3, in new tabs, the breadcrumbs should appear as follows for tab 1 and tab 2, respectively:
HomePage/Data_2/
HomePage/Data_3/
In the current implementation, I managed to update the state of breadcrumb whenever new links are opened such that breadcrumb[0] would be equivalent to HomePage while breadcrumb[1] was initially Data_1, then became Data_2, and lastly Data_3. Hence, the last value of breadcrumb[1] is Data_3 since that's the last opened link. My problem is that whenever the user refreshes previously opened tabs/pages corresponding to Data_1 and Data_2, since they are all using a single store and breadcrumb[1] has been changed to Data_3, the breadcrumbs in Data_1 and Data_2 pages also become Data_3.
In this case, I can just think of using multiple stores since I perceive that it could be the only solution given my use case. Meanwhile, I can't find any sufficient documentation online regarding using multiple stores in redux. Maybe I can pass the store to the next page in params...? something like that
Can someone please help. Thanks in advance.
To emulate per-tab same site persistency, I would, right before the page is about to be refreshed (window.beforeunload or similar)
1 - Write to localStorage about my breadcrumb
2 - Refresh
3 - Read from localStorage about my breadcrumb to initialize myself properly (so the data here SHOULD be mine because i just wrote it)
4 - remove breadcrumb information from localStorage (to prevent other tabs from reading it)
Now, you still have the case where the user just closes the tab, so you would have stale data about the breadcrumb in your localStorage. You can add a little expiration mechanism (you might consider that data in the localStorage older than 10 seconds is stale and just pretend it isn't here and delete it at step 3.) Cookies would work pretty much the same, with a built-in expiration mechanism.

How to use two arrays together in k-object tag in Kitsune?

We are calling every dynamic data under location's schema except business updates. Everything is fine except update-details page because, in update details page we have the k-object tag of updates.
When we are moving out from update-details page to any other page, where k-object tag is of location then the k-dl tag of with location is not accessible.
For e.g.
URLs before getting into update-details page.
www.example.com/bhutan/about-us/k._id
URL after getting into update-details page.
www.example.com//about-us/
Please help...
If i understand this question right, you want to access object in another array while you are navigating from one details page to another ?
In this case, Kitsune allows you to access any array in any page. Say you have a page with:
k-object="[[update in Business.updates]]"
in the same page you want to navigate to another page, which is details page of a product, then -
Click Here
Let me know if this answers your question, else do give me code examples for me to give a clearer answer.

ZK window not unique in ID space

In our project we use ZK for webpages. There is a combobox which has lists. When selected, it fetches data from a java object through onSelect, i have given the logic.
when i select one there are 4 listboxes on that page to be filled with data according to the selection. when i select first time, no problem occurs.
But on second time i get an error pop-up like "Not Unique in the id space of Window" and showing the list box item id which have to be filled on select.
Can any one help out there?
Note: Though it shows this error i get the listboxes filled correctly according to the combo box selection. Still i cant stop this error occurring..
Your issue is a conflict of ids in ZK's id space.
A bit of background..
ZK generates ids for components at runtime, if you open up the DOM in the browser you'll see each component has some machine readable id.
However, you can also give components an id. This id does not go to the DOM but lets you reference the component in your application.
These two shouldn't be confused. The conflict you're experiencing is with the latter type of id; you are assigning a component an id in your Java code or in your ZUL file which, at runtime, is not unique.
The case you describe where it only happens the second time you click is a tell tale sign here. The content you are adding on the event has an id defined in it and you are not removing this content when you are done.
Consider the following example:
#Wire
private Window myWindow;
#Listen(Events.ON_CLICK + " = #myButton")
public void onMyButtonClicked() {
Label myLabel = new Label("sean is cool");
myLabel.setId("myLabel");
myLabel.setParent(myWindow);
}
This will work the first time you click myButton, but will throw your error on the second click. That is because the second event tries to add myLabel to myWindow but there is already a myLabel there.
There are lots of ways to resolve this depending on what you are trying to do.
Have a look through the ZK documentation on ID Spaces for more.
I also faced the same error.
My scenario is the same, only the widgets being used are different.
Hence putting my workaround here.
I have put the following piece of code in the doBeforeCompose() method of the composer:
Component widgetWithId = page.getFellowIfAny("widgetWithId");
if (widgetWithId != null) {
widgetWithId.detach();
}
Here widgetWithId is the component/widget, which is tried to be regenerated by the code, with the same Id and ZK is throwing error for it.

Render a page differently based on which list that opened it

I have two lists of different PageTypes - NewsItems and PressReleases. They are displayed in one list each, with links to the individual items.
Now I want to include the press release items into the news list and use the style of the news items to display them as news items. They share properties like "Heading" and "BodyText", which are used in the News Template.
I imagine that it won't be that difficult to feed the NewsItems' ListPage with both sets of pages, but I don't understand how I can control the rendering of the item page.
I would like to take the PageData object from a NewsItem OR a PressReleaseItem and display it using the News-Item.aspx template, if it is selected in a NewsList. But EPiServer will always render the PressReleaseItem with the PR-Item.aspx since it's coupled in the PageType settings.
Anyone know how to accomplish this?
EDIT: An effort to clarify:
The important issue is how to know the "list parent" and choose the right template from that. In the ListPage I can apply different looks on the PR and News items respectively using tompipes answer, but when selecting to see an individual item EPi will render the PR-Item-1 the same way regardless of their "list parent". That's the problem.
I'm not following exactly what you are attempting here. But I think I get the gist of it.
Why not use one aspx template for both page types, but in the code behind switch off sections using the visible attribute.
If you are using PageTypeBuilder you could use the "is" keyword:
somePlaceHolder.Visible = CurrentPage is NewsItemList;
If you're not using PTB, you could use something like:
somePlaceholder.Visble = CurrentPage.PageTypeID == 10;
or
somePlaceholder.Visble = CurrentPage.PageTypeName == "NewsItemList";
I'll point out now I'm not a fan of hardcoding anything, so I would place the template name, or ID into a config file, or a property on the start/root page to avoid hardcoding them.
Let me know if this will help, or if I have misunderstood please try elaborate on your issue.
Depending on how much the templates share you could go with user controls, placeholders or even different masterpages to switch view in a suitable way.
To know when to switch you could use a querystring parameter, session variable or the nicest looking way would probably be to lookup and get the list's PageData object by the HTTP referrer. If it's empty you will get press release rendering as the worst case.
I tried lots of solutions, including the adding of querystring to the PR items in the list links, the getting of referring url in the item template and different types of event hooking for automatic publishing of news items from a PR item (although I only looked at the code samples for that one), and finally came to the conclusion that they all had something that told me not to go that way. (Making the code too complex, or the markup logic too hard to understand and so forth)
I ended up using Fetch data from another EPiServer page, and creating a "shortcut pagetype" in which I let my editors pick which PR item should be used as base for a news item.
This shortcut pagetype is called "PR-as-news-itemPage" and it is rendered with the same aspx as ordinary news items: News-Item.aspx. Having no properties of its own, it will take all relevant data from the PR item selected with "Fetch..."
To render PR items with all its properties I created an ordinary new pagetype called PR-Item.aspx. This renders the "Attribute 2" property, which is only rendered by PR-item.aspx, and not by News-Item.aspx.
(I could have gone even simpler, by letting the editors use the old News-Item page type and use the "Fetch..." property there, but I have some mandatory properties in that page type which I didn't want to make optional for this sake.)

Resources