How to open a new tab in same session - google-chrome

How to open a new tab in same session, I have already tried common solution given by others but its not working.
I am trying in chrome browser.
My project is in java

To open a New Blank TAB you can use the following line of code :
((JavascriptExecutor) driver).executeScript("window.open('','_blank');");
To open a New TAB with url you can use the following line of code :
((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");

Related

How to open new tab in browser (in same browser window) with TestCafe?

I am currently unable to open a new tab in the same browser window. Please refer scenario.
After login, the application opens the page in the same browser
window. I need to click on the card and after it will open in a new
tab and where I need to test everything. Currently, I need to
simulate a direct URL for a new tab but I want to open a new tab with
test case code.
Please let me know is it possible in the test cafe?
Thanks.
Yes, you can pass --disableNewWindow in testcafe/runner argument to open url in same tab.
Also, refer to https://testcafe.io/documentation/402841/guides/advanced-guides/multiple-browser-windows#disable-support-for-multiple-windows
--disable-multiple-windows

Opening a PDF from a link in a new tab

I'm trying to open a PDF file in a new tab (or window depending on the browser config).
Currently if I specify the href for a local file it works as expected, but I need to provide a link that dynamically generated by an API.
So basically:
PDF--> this works (opens the file in a new tab)
PDF--> this opens a new window, closes it immediately and download the file.
I believe this is a security measure by the browser, but is there any way to get around this behavior and show it in a new tab?
You can use JS window.open to open the PDF in a new tab:
<a onclick='window.open("https://the_URL_here.somewhere.some","_blank")'>Open PDF in a new tab</a>
Or call a function that runs the window.open command. Another way would be to generate the link and add it to a layer using getElementById() using the innerHTML property.

browser.pages() does not switch to newly opened tab in puppeteer

I am trying to open a new tab and scrape information from the new tab using puppeteer. I am doing
pages = browser.pages();
new_tab = pages[pages.length - 1]
new_tab.screenshot({path: "sample.png"});
to switch to the new tab and take a screenshot. However, puppeteer doesn't switch to the new tab and takes a screenshot of the old tab instead. After some debugging I realized that the new tab isn't added to pages for some reason. Specifically, I used pages.length() which returned 2, pages[0] manipulates the pre-existing blank page and pages[1] = pages[pages.length()-1] manipulates the previous page.How do I fix this?
Thank you

How to open new window instead of new tab in chrome using webdriver?

In order to automate my test application, I need to open few links in a new window instead of tab. Keep this in mind that I am not opening the links in new tab explicitly, it is my web application which automatically lands user in the new tab after clicking on the link.
Why do I want to do this?
Because running the tests on chrome browser closes the main tab and keeps open the newly opened tab. Which ultimately fails the tests.So ultimate intention is to open the new window instead of tab and handle it properly using driver.getWindowHandles().
What have I done so far?
I tried to find some kind of capability setting or profile in Chrome which automatically opens the links in a new window which are supposed to be open in a tab.But did not find any convincing solution most of the suggestions are CTRL+CLICK ON THE LINK.
I'm not a guru of web-design, but I can suggest following scenario:
// Get required page
// Excecute below JavaScript with JavaScriptExecutor
var reference = document.querySelector('a#someID').getAttribute('href'); // You can use your specific CSS Selector instead of "a#someID"
document.querySelector('a#someID').setAttribute("onclick", "window.open('" + reference + "', '', 'width=800,height=600')")
document.querySelector('a#someID').removeAttribute('href')
// Find target link
// Click on it
This code should allow you to make changes in HTML source code of target web-element to force its opening in new browser window.
Note that with this code element's appearance on page will be changed until page refresh
P.S. You didn't mentioned your programming language, so there is no complete implementation... However, this is Python implementation example:
from selenium import webdriver as web
dr = web.Chrome()
dr.get('https://login.live.com/login.srf?&wreply=https%3a%2f%2foutlook.live.com%2fowa%2f%3fnlp%3d1%26realm%3dlogin.live.com')
dr.execute_script("""
var reference = document.querySelector('a#ftrTerms').getAttribute('href');
document.querySelector('a#ftrTerms').setAttribute("onclick", "window.open('" + reference + "', '', 'width=800,height=600')")
document.querySelector('a#ftrTerms').removeAttribute('href')
""")
link = dr.find_element_by_id('ftrTerms')
link.click()
Well, in the absence of any flag/setting/capability in Chrome browser which opens the links in a new window instead of the new tab I used a Chrome Extension for that via WebDriver.
Why did I do that?
Because my tests are running fine on Firefox and I have no idea how many WebElements are there in the suite which gets open in new tab in Chrome browser. The suite is also very huge so doing any changes in its core page class may break the all the tests.In addition to that, changing code at an element level will be very time-consuming and most importantly not a generic solution.
What did I do?
I used a chrome extension New Tab New Window, which opens all the new tabs into a new window.
Downloaded the CRX file of this extension using an extension Get CRX.
Set the CRX file as a capability of Chrome.
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("pathOfCRXFile"));
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
So above will convert all the new tabs into a new window.So whenever the driver clicks on any link which further opens in a new tab will get opened into the new window.

Can't open in new tab in Sublime Text 3. Is it a bug?

Today my Sublime 3083 became corrupted! I don't know why but every time I click on a file it opens on the same tab!
I cannot open two tabs at the same time. Fist file opens in a new tab and another opens in the previous tab and so on!
I did not installed any plugins recently. I just selected "Close Other Tabs" on menu.
What can I do?
Thanks
You must open the file using double click. If you open it using single click then you're using a feature called file preview, and that file will be closed when you preview other file unless the file has changes.
In addition, in the settings file you can set the preview_on_click option to false to disable the preview option; if you do that a click on the file won't do nothing, and a double click will still open the file in a new tab. As far as I know there's no option to open the file with a single click, only you can 'preview' the file (it's similar to open but the file get closed if you preview another file). Example setting:
{
"preview_on_click": false
}
I was facing a similar but little different problem. I was not able to view any tabs I was opening, and it was always opening a new file.
I fixed it by "View" -> "Show Tabs".