Switch to another window in Cypress - 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
})
})

Related

RN Web, href in TouchableOpacity: onPress >navigation, onRight click > context menu with possibility to open link

I have a site with TouchableOpacity that uses react-navigation to navigate to another screen. Is it possible in some way to add href to this button so I could open the another screen in new tab using context menu "Open link in new tab"?
I know there is possibility to add accessibilityRole='link' href={''} to a component but what about a whole button of view.
This is according to: https://github.com/necolas/react-native-web/issues/162
Using Text component it is possible by:
<Text
accessibilityRole='link'
href={defineRightClick()}
target='_blank'
onPress={(e) => {
e.preventDefault();
navigateFunction();
}}>
Click to navigate, right click to open in new tab
</Text>
Ask if more information is needed for this question and I will edit it.
Every help is appreciated as I have tried to find solution but have not come across or found a way to handle this case.
Found an answer!
Even thought it is not documented in here: https://necolas.github.io/react-native-web/docs/accessibility/#accessibility-patterns
and using href in TouchableOpacity will show ts error No overload matches this call., it is possible to add to a TouchableOpacity props
accessibilityRole='link'
href={'desired link'}
target='_blank'
Then using e.preventDefault() in onPress event of TouchableOpacity will prevent link opening and do other things assigned to the function. At the same time the link is possible to be opened with right click > context menu > "Open link in new tab"
I will post this as answer so if anyone else comes across this they might find the solution

Laravel dusk coming back to original browser tab or close latest tab but keep original

I have situation where I need to click the link and visit new tab and see if there is that particular text or not and then come back to original tab and perform some work.
this is the code I am using to confirm I am in latest browser tab after clicking view button
$window = collect($this->driver->getWindowHandles())->last();
$this->driver->switchTo()->window($window);
Now I have to come to original tab, How can i do that ? Any help?
is there any way to close recent tab after some time. so that i can come to original tab.
Thanks
You're on the right track.
You can continue with something like this:
$this->browse(function (Browser $browser) {
$browser->visit('/')->click('#some-link');
// switch to the last tab
$window = collect($browser->driver->getWindowHandles())->last();
$browser->driver->switchTo()->window($window);
// make some assertion on that tab
$browser->assertRouteIs('some-route.alias');
// switch back to first tab
$window = collect($browser->driver->getWindowHandles())->first();
$browser->driver->switchTo()->window($window);
// TODO: make further assertions
});

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.

Targeting a new tab in Selenium

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

How to call the same function that the ToolBar editbutton does

So I have created a context box upon right click that has Add/Edit/Delete Rows. I also have a bunch of code launched before the Dialog is shown. My problem is that when I use the context menu it doesn't go through some of the code. I have tried to call on the functions directly but it doesn't format correctly.
I am mainly concerned with the edit button, here is the code I am using to bring up the edit Dialog
function editRow() {
var grid = jQuery("#<%= Jqgrid1.ClientID %>");
var rowKey = grid.getGridParam("selrow");
if (rowKey) {
// I have tried calling functions here and it still doesn't work
grid.editGridRow(rowKey, grid.editDialogOptions);
}
else {
alert("No rows are selected");
}
}
So if I use this to display the editform it isn't formatted correctly nor does it go through the functions all correctly.
I am using the ASP Webforms version of Jqgrid so I call the function by doing this
<cc1:JQGrid1 ID="Jqgrid1
//other attributes
ClientSideEvents-BeforeEditDialogShown="ChangeMonitor"
//Rest of code />
So this works just fine, and I'm trying to get the Edit button on the context menu to display correctly.
My thought was to use Jquery to trigger a click on the actual Edit button once someone used the context menu. I couldn't find an ID that would work however.
Is there an easy way to connect my context menu Edit button, with the actual Edit button in the toolbar?
Well I found a solution to my problem.
The id field of the button was edit_ct100_cpMainContent_Jqgrid1_top so I just triggered a click with this code.
$("td[id^=edit][id$=top]").trigger("click")
For some reason when I used the _ct100_cpMainContent_Jqgrid1 it wasn't working, but now it does. Hope this helps someone.

Resources