Webdriver - Opening a new browser with the same profile - junit

I am running some java tests for a web application with Webdriver on Firefox, trying to verify the functionality of a "Keep me logged in" button. Each time a new browser is launched, it is done so with a new profile. Is there a way to instruct it to open the new browser with the same profile as before, the one that logged in and pressed on "keep me logged in"? I'm sorry if the question doesn't make sense, I'm still a bit new at this.
Thanks,
Ragnar

This isn't currently possible. WebDriver will use a fresh profile every time you launch the browser (although you can use an existing profile as a model). You can file a feature request in the Selenium tracker

You can use FirefoxProfile with an existing profile:
FirefoxProfile profile = new FirefoxProfile(path to profile dir);
WebDriver driver = new FirefoxDriver(profile);

You can not do that with obtaining a new WebDriver instance.
but you can do that with JavaScript as below
((JavascriptExecutor)webDriver).executeScript("window.open('"+ConfigLocator.getTargetServer()+"', '_blank');");

First of all, like Jarib said, it is possible to use an existing profile as a model. It means no specific data like favorites, history, homepage will be really loaded. But there is a possibility to set some special sytem properties to driver.
webdriver.reap_profile
Should be “true” if temporary files and profiles should not be deleted
System.setProperty("webdriver.reap_profile", "true");
Most probably there is a possibility to use that kind of solution in your situation too.

Related

Permanently blocking a network call / url on IE, Edge, Chrome or Firefox

I have a webpage that I have to work on all day, it's a portal. I need to reload it about 500 times a day, at least.
I find it to be painfully slow, and I have identified that it makes a network call (just to load a profile picture) to a particular website that I cannot list here, and that network call timesout.
Now because it times out, I don't get a profile picture anyway, but it ends up wasting about 30 seconds per reload.
I can block the domain in the "Networks" tab of Chrome Dev Tools, but I am looking for a more permanent fix. I don't want to have the Dev Tools open all the time since it uses precious screen real estate. I haven't found out a way to permanently block that particular network call which will save me hours per day.
There are 3 things that come to my mind right now:
Block all image assets on the webpage
Initialise the chrome instance using Selenium maybe, and pass in the option to block that network call.
Block that particular website? It hasn't worked for me so far, am I doing it wrong?
I'm comfortable with a fix on any browser (Internet Explorer, Edge, Chrome, Firefox).
Any leads on this one? I can't be the only person to face this, and yet I haven't found out a solution for it without using admin access, which I don't have.
Workaround 1: Call the chrome instance from Selenium, and disable all images. Works, but not the best approach.
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)
Workaround 2: Launch Chrome Window from Selenium while executing Network.setBlockedURLs. It will only work in Selenium 4 and above, but it's not working as of now on Selenium 4.0.0.b3.
https://chromedevtools.github.io/devtools-protocol/tot/Network/#method-setBlockedURLs
from selenium import webdriver
driver = webdriver.Chrome()
driver.execute_cdp_cmd('Network.setBlockedURLs', {"urls": ["https://www.somelink.com/*"]})
driver.get("www.mywebsite.com")

Start Chrome emulation from command line

I use Google Chrome with Intern to run automated tests and I would like to know if there is a way to launch Chrome in emulation mode from CLI or using a specific flag to test mobile rendering. If not, do you know a good workaround ?
I could directly use the Android Emulator (from Android SDK) with Selenium Webdriver apk or with mobile Chrome but tests are crashing most of the time, emulators don't respond and I have to restart it. Also, I need to test on the largest possible scope, not limited to Android devices.
Chrome on desktop is a lot more stable and even if a test fails, chrome always respond and can be closed automatically by Intern.
I tried a workaround with the "--enable-touch-events" flag and with a custom userAgent but it's producing weird behaviors. Maybe some other flags would help me ?
Thank you in advance for your answer.
This is currently not possible in Chrome.
It's a feature I've been wanting myself too so I've gone ahead and filed a feature request for it at the following link:
https://code.google.com/p/chromium/issues/detail?id=373169&thanks=373169&ts=1400050662
I'm crossing my fingers but it wouldn't hurt if you and other people interested in this went and left a comment on the thread too. The more people asking for it, the higher the chance of it being implemented. And it does seem like it would be trivial to implement since it currently only takes a couple of mouse clicks to enter emulation mode.
Selenium allows users to emulate Chrome on a mobile device using code like this:
Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", "Nexus 5");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
WebDriver driver = new ChromeDriver(chromeOptions);

retaining cache in firefox and chrome browser - Selenium WebDriver

Currently our web application takes around 3 mins to load completely without caching and 10 secs with caching. When I open the app through WebDriver its taking around 3 mins to load i.e. caching is not used. I observed this on Firefox and Chrome browser. Not sure how to enabled the driver to use cache instead of loading each file from server every time I open the app.
Here are the things I tried.
1. disabled clearing cache on browser exit in browser setting.
2. set 'applicationCacheEnabled' desiredcapabilitiy to 'true'
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setCapability("applicationCacheEnabled", "true");
WebDriver d = new FirefoxDriver(cap)
But nothing seems to work. Please let me know how can I make webdriver to use caching.
The problem is, that selenium copies every startup a new (firefox/chrome) profile to the temp directory and starts firefox/chrome with it.
However, it is possible to always use the same profile for your test instances.
I think this way you can get it working faster.
For firefox you just need to do these steps:
1. Load your webapp in a selenium firefox instance and don't close it afterwards (not driver.close();).
2. Then go to Help->Troubleshooting Information and open the folder under Profile folder.
3. Copy its content to a new folder near your test code.
4. Load the saved Profile in your test code. You can do it this way:
FirefoxProfile profile = new FirefoxProfile(new File("profile/folder/path"));
WebDriver driver = new FirefoxDriver(profile);
I think you can do this in chrome analogous.
#CacheLookup annotations can be very useful for the elements that do not change on the web page once loaded. These types of elements constitute a majority of elements on the web page. So for those elements, as they will not change during test execution, you should use the #Cachelookup annotation to improve the test speed.
Try below codes:
#FindBy(name="username")
#CacheLookup
private WebElement userName;

Adobe Air - Open app in fullscreen

I created an Air desktop app with Flash CS 5. Usually Windows (XP) is opening an application (like Firefox) with the latest set size and position.
For my installed Air app it's always just the default one.
How to start it with the latest used size and position?
Thanks.
Uli
hope this will work for you:
http://cookbooks.adobe.com/post_Using_the_FullScreen_functionality_in_AIR-8004.html
http://blog.ochodurando.com/2010/04/adobe-air-e-fullscreen/
You need to save a record somewhere that remembers the window's size, and possibly position. If your app has a preferences file, this would be an ideal place to store that information. Then, whenever your app starts, it checks for this information and resizes the window if any values are found.
Most popular programs include this feature (and don't even mention it, since it's pretty basic UI), but it's done intentionally and not as a default for every application. Thus if you want it, you have to program it in.
You can read and write to application.xml. You'll find there and nodes.
file = new File( File.applicationDirectory.nativePath + "/META-INF/AIR/application.xml" );
Adobe restrict writing access to application diractory but this trick is useful if you don't want to create a separate config file in app-storage:/ folder, which is of course prefered.

Perl Mechanize module session cookie issue

I try to use the perl script to automate the interaction with a website.
I use module WWW::Mechanize to realize my design. But, the website [https] shows session expired.
Can anyone help me with this problem.
First you need to try to login to the same site via browser but with Javascript disabled.
If you'll get same error page this mean that some cookies (or some redirects) are added via Javascript so you need to add these cookies manually from your code.
Any way i recommend you to install HttpFox for Mozilla Firefox and record you login session after that you can find what's wrong with the target site.
Did you
my $browser = new WWW::Mechanize;
$browser->cookie_jar({});
?