Targeting a new tab in Selenium - ruby

I have a test which essentially will click a link on the footer of the page. We have three of them and they all lead to different places:
A Google Docs form
A training page
A licensing page
The code below makes the right assertions and clicks the link:
if #link_selection.eql?('leave feedback')
#wait.until {#driver.find_element(css: => 'cell.feedback').click}
#wait.until {#driver.find_element(css: => 'a[href="https://docs.google.com/a/showmyhomework.co.uk/forms/d/1LP8BZ950TSXXDR1HuVz7yhv9Cp3h6scmQtNFqIRW_XI/viewform"').click}
puts "Leave feedback link clicked"
I've modularised it for each different link location.
When the link is clicked, it naturally opens in a new browser tab. I then wanted to extend the test to then view the tab which was opened and then make an assertion on that page. I wanted to ask how I can handle a new tab in Selenium and the way in that:
a. It asserts that the new tab is open (and switches to it)
b. It can then assert a heading or title of the page (so that the test is sure that the page has been opened.

The following code in c#, please feel free to change it to ruby.
// This gets the window handles of open available windows(also, tabs)
var availableWindows= driver.WindowHandles;
To switch to the new tab with respect to the title of the page:
string Title= "whateveryourpagetitleis";
foreach (string windowId in availableWindows)
{
string windowTitle = driver.SwitchTo().Window(windowId).Title;
if (windowTitle.ContainsIgnoreCase(title))
{
driver.SwitchTo().Window(windowId);
}
}
You can play around with the functions and then use your asserts accordingly. But, what you're looking for is WindowHandles

Related

How to switch to another Teams tab calling button onclick JS/TypeScript

I am programming app with more MS Teams tabs. In first tab i have button, which should lead to switch to other Teams tab.
In TeamsToolkit I made second tab with onclick on
I'm trying to call pages.tabs.navigateToTab function:
const doClick = (evt: React.MouseEvent) => {
const tabInstance: TabInstance = {
entityId: "index0", tabName: "Personal Tab", url: "index.html#/tab", websiteUrl: "https://localhost:53000/index.html#/tab"
};
pages.tabs.navigateToTab(tabInstance);
}
but only content changes, I miss change in "selected" item in top menu:
Am I doing something wrong? Or is it even possible to manipulate "selected" item in tab list?
Another tab is another application basically (maybe not even yours, like "Files" for example or "Posts" tab). You can tell teams to activate another tab using so-called "deep links". I am not sure, how to do it with teams tookit, but with teams API you just call executeDeepLink (v1) or pages.navigateToApp (v2) with corresponding link.
Read more about deep links here:
https://learn.microsoft.com/en-us/microsoftteams/platform/concepts/build-and-test/deep-links

Switch to another window in Cypress

I want to switch to to a new window on pressing a button, and the new window has dynamic URL, How do we handle this. Is there any workaround. I searched through many articles
Click the add button
cy.get('a[href*="javascript:xxxSearch();"]').click()
It opens a new window
I want to access(search/add the elements in new window and then switch back to previous main window
May be a good idea if you could share the full html of that page. As I understood from the question, may be grab the href and then get the attribute of href to a const and visit to that link as below. Just give a try and let me know if this is working for you.
it('Open a new window',function(){
cy.visit('some_url')
//test code continues....
cy.get('a[href="/some/link"]').then(($href)=>{
const hrefLink = $href.attr('href');
cy.visit(hrefLink);
// rest of you test to grab the search button
})
})

How to verify element displays in new window after clicking a link on the main window

I am using Cypress to click a link on the Homepage which opens the respective page in the new window. I am not able to identify any element on this page. I am doing this to verify that the click opens the correct page.
I saw the link: Access a new window - cypress.io which didn't help much to answer the problem.
Please suggest if there is some way to test this.
I have tried few things like:
cy.window().contains('.div.trItem')
cy.window().its('.trValue).should('eq','SomeHeading')
cy.url().should('include', '/theLink1.html/')
Expected:
- Clicking link open the correct page in new window
- Identify URL in new window includes some text
- Certain text displays on the new window
First of all, you can't access to another tab or window with Cypress. Consider to separate your test cases. For example, in one test you could check that a element contains href attribute with expected url and target attribute with _blank.
Like this:
it('contains valid a element', () => {
cy.visit('https://example.cypress.io/')
cy.contains('Cypress.io').should('have.attr', 'href', 'https://www.cypress.io').should('have.attr', 'target', '_blank')
})
And in second test check that your other page contains expected text.

Link directly to a notebook page in a view

I have an view that extends the current project view, where we add multiple tabs (notebook pages) to show information from other parts of a project.
One of these pages is an overview page that summarizes what is under the other tabs, and I'd like to link the headlines for each section directly to each displayed page. I've currently solved this by using the index of each tab and calling bootstrap's .tab('show') method on the link within the tab:
$(".overview-link").click(function (e) {
e.preventDefault();
var sel = '.nav-tabs a:eq(' + $(this).data('tab-index') + ')';
$(sel).tab('show');
});
This works since I've attached a data-tab-index="<int>" to each header link in my widget code, but it's brittle - if someone adds a tab later, the current indices will be broken. Earlier I relied on the anchor on each tab, but that broke as well (and would probably break if a new notebook page were inserted as well).
Triggering a web client redirect / form link directly works, but I want to show a specific page in the view:
this.do_action({
type: 'ir.actions.act_window',
res_model: 'my.model.name',
res_id: 'my.object.id',
view_mode: 'form',
view_type: 'form',
views: [[false, 'form']],
target: 'current'
});
Is there any way to link / redirect the web client directly to a specific notebook page tab through the do_action method or similar on FormWidget?
If I understood well you want to select the tab from the JavaScript (jQuery) FormWidget taking into account that the id could change if anybody install another module that adds another tab
Solution 0
You can add a class to the page in the xml form view. You can use the id of the element selected by this class name in order to call the right anchor and select the right tab item. This should happen when the page is completely loaded:
<page class="nb_page_to_select">
$('a[href=#' + $('.nb_page_to_select').attr('id') + ']').click()
NOTE: As you have said the following paragrah I assume that you know where to run this instruction. The solution I suggest is independent of the index.
This works since I've attached a data-tab-index="<int>" to each
header link in my widget code, but it's brittle - if someone adds a
tab later, the current indices will be broken. Earlier I relied on the
anchor on each tab, but that broke as well (and would probably break
if a new notebook page were inserted as well).
Solution 1
When the page is loaded you can get the tab list DOM object like this:
var tablist = $('ul[role="tablist"]')
And then you can click on the specifict tab, selecing by the text inside the anchor. So you don't depend on the tab index:
tablist.find('a:contains("Other Information")').click()
I think if you have two tabs with the same text does not make any sense, so this should be sufficient.
Solution 2
Even if you want to be more specific you can add a class to the notebook to make sure you are in the correct notebook
<notebook class="nt_to_change">
Now you can use one of this expressions in order to select the tab list
var tablist = $('div.nt_to_change ul.nav-tabs[role="tablist"]')
// or
var tablist = $('div.nt_to_change ul[role="tablist"]')
Solution 3
If the contains selector doesn't convince you because it should be equal you can do this as well to compare and filter
tablist.find('a').filter(function() {
return $.trim($(this).text()) === "Other Information";
}).click();
Where "Other Information" is the string of the notebook page
I didn't tried the solution I'm giving to you, but if it doesn't work at least may be it makes you come up with some idea.
There's a parameter for XML elements named autofocus (for buttons and fields is default_focus and takes 1 or 0 as value). If you add autofocus="autofocus" to a page in XML, this page will be the displayed one when you open the view.
So, you can try to add this through JavaScript, when the user clicks on the respective link -which honestly, I don't know how to achieve that by now-. But you can add a distinctive context parameter to each link in XML, for example context="{'page_to_display': 'page x'}". When you click on the link, I hope these context keys will arrive to your JS method.
If not, you can also modify the fields_view_get method (here I wrote how to do that: Odoo - Hide button for specific user) to check if you get the context you've added to your links and add the autofocus parameter to the respective page.
As you said:
This works since I've attached a data-tab-index="" to each header
link in my widget code, but it's brittle - if someone adds a tab
later, the current indices will be broken.
I assume that your app allow multi-user interaction in realtime, so you have to integrate somewhere in your code, an update part function.
This function will trig if something has changed and cleanout the data to rebuilt the index in order to avoid that the current indices will be broken.

iFrame tabbing within frame/pop-up window

As shown in the following example, I would like to restrict the tab key behavior to just this frame with links.
http://jsfiddle.net/zFXNM/
tabindex ="1"
I do NOT want the tab to go the URL other items in the browser.
For example, the "Compose Mail" in Gmail does this already. I observed 3 usages of "tabindex=-1" within the JS.
In pure JavaScript, I have figured out a solution for this issue. The idea is whenever the page is loaded we determine the first and last links, then we prevent the tab default action and jump to the first link from the last tab.
if (activeElement.id == lastHrefID) {
e.preventDefault();
document.getElementById(firstHrefID).focus();
}
A working solution is available at : https://jsfiddle.net/srirammac/95sv63s7/

Resources