Safari mobile emulation for web app via Selenium - selenium

Need to test responsive design for web app. I understand that browser didn't emulate like real device or even device emulator, but i don't need it.
Note: i'm looking for not to run Safari inside emulator, but to activate mobile emulation in Safari
question 1:
Is it possible to start Safari via selenium with predefined mobile emulation, like in Chrome here
Map<String, String> mobileEmulation = new HashMap<>();
mobileEmulation.put("deviceName", "Nexus 5");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("mobileEmulation", mobileEmulation);
WebDriver driver = new ChromeDriver(chromeOptions);
if answer no(assume it will be no), question 2:
is it enough to resize Safari window to required device viewport size?

Setting the browser width isn't enough as many of today's web pages use the useragent as a means to determine the device that is viewing the page. Afaik, there is no way and I've been trying to find a solution to this for a while. Safari doesn't give automated control to set the user agent nor the mobile mode of its desktop browser.
There is a way to do what you want here though: https://webkit.org/blog/9395/webdriver-is-coming-to-safari-in-ios-13/
But the requirement is you need an iPhone connected to your laptop to run the test from and you need to add these capabilities to your configuration
safari:deviceType
safari:useSimulator
Read the man file for safaridriver for more info.
If you need to set the useragent, you can do it via the command line, but you need to reset it after https://tarunlalwani.com/post/selenium-change-user-agent-different-browsers/

Related

Appium: How to switch in Mobile App from native App to Mobile Web Browser and run URL on Browser in mobile

I have a native mobile App and automated by using a Appium Driver (AppiumDriver driver) which is working OK and by one Feature of App e.g. unlock Account send an Email to user which need to be confirmed by User.
So I have find out how to get the link in Email and how to get the URL from send Email.(saved in a String var e.g. "href")
So when the automation run I am in Mobile App in window "INFORMATION" which inform user we have send an Email to confirm.
Now my question is
How can I switch from native mobile App (screen INFORMATION)and Put the URL in Browser and after that should open in Browser a Window which ask user enter new password and repeat it and then press OK ?
After that I should again back to the Mobile App.
Do I need another driver like a Web Driver (additional to appium Driver mentioned above) to handle the Actions in Browser after switch?
Thanks for any Support
This my Capability:
public AppiumDriver<MobileElement> driver;
.....
public DesiredCapabilities cap = new DesiredCapabilities();
.....
cap.setCapability("deviceName", helper.getConfiguration(configFileName, "androidDeviceName"));
cap.setCapability("platformName", helper.getConfiguration(configFileName, "androidPlatformName"));
cap.setCapability("PlatformVersion", helper.getConfiguration(configFileName, "androidPlatformVersion"));
cap.setCapability("automationName", "uiautomator2");
cap.setCapability("MobileCapabilityType.FULL_RESET", android_mct_fullReset);
cap.setCapability("MobileCapabilityType.NO_RESET", android_mct_noReset);
cap.setCapability("appium-version", helper.getConfiguration(configFileName, "appiumVersion"));
cap.setCapability("language", helper.deviceLanguage);
cap.setCapability("locale", helper.deviceLocale);
cap.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, "true");
cap.setCapability("app", app.getAbsolutePath());
....
driver = new AppiumDriver<MobileElement>(new URL("http://localhost:4723/wd/hub"), cap);
I think this will be achieved with :
driver.startActivity(new Activity("com.example", "ActivityName"));
Import by :
import io.appium.java_client.android.Activity;
With this method you can switch application, you must know the APP_PACKAGE and APP_ACTIVITY that you have, try this link or this link to learn this.
This is example the APP_PACKAGE and APP_ACTIVITY google chrome browser from play store :
driver.startActivity(new Activity("com.android.chrome", "com.google.android.apps.chrome.Main"));
You don't need to make a new initialize driver for the browser, just do it.
Your native apps start here
...
Switch to browser
//example chrome
driver.startActivity(new Activity("com.android.chrome", "com.google.android.apps.chrome.Main"));
....
//back to last activity your native app
driver.startActivity(new Activity("yourAPP_PACKAGE", "yourAPP_ACTIVITY"));
You can use Activity class to launch another application using AndroidDriver.startActivity() function like:
Activity activity = new Activity("activity.package", "activity.name");
activity.setStopApp(false);
((AndroidDriver<MobileElement>) driver).startActivity(activity);
An easier way would be just going for Launch command available via SeeTest Appium Extension like:
seetest.launch("activity.name", false, false);

webGL dependent application in opening in chrome

I have a situation here that we have a webGL dependent application to automate. i have automated the whole application on my laptop, and this application is running fine on chrome on my laptop. but the issue is when i run this application from a seperate machine which is a selenium GRID node. It gives me a message that the application required Webgl and and it ask me to enable the webGL. i have tried different methods like selenium option and ignoring the chrome black list as well.
when i open the browser manually and open the application it runs find and i checked on url chrome://gpu. it shows that the gpu is enaled.
but when i invoke the selenium and run the test cases,. the browser initiates with WebGl as disabled
i found the solution to this. here is the code you can use to enable WebGL for chrome.
DesiredCapabilities capability = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
**options.addArguments("--ignore-gpu-blacklist");**
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capability.setCapability("chrome.binary", "C:\\Temp\\chromedriver.exe");
capability.setVersion("9999");
capability.setCapability(ChromeOptions.CAPABILITY, options);
return new RemoteWebDriver(HUB_URL,capability);

How to test my selenium web test cases on mweb

I have few selenium test cases; running fine on desktop website. My website is responsive; hence works fine on Mobile Browser also. How do I test my test cases on Mobile Browser ?
If your requirement is to test your site in Mobile view (As your site is responsive) then following code may help you -
FirefoxProfile ffprofile = new FirefoxProfile();
ffprofile.setPreference("general.useragent.override", "iPhone"); //this will change the user agent which will open mobile browser
WebDriver driver = new FirefoxDriver(ffprofile);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().setSize(new Dimension(360,640)); //just to change the window size so that it will look like mobile
driver.get(siteURL);
You can use Appium to support the
Mobile web apps are web apps accessed using a mobile browser (Appium supports Safari on iOS and Chrome or the built-in ‘Browser’ app on Android).
Its design is based on the WebDriver API, so you’re free to use whatever test runner and test framework you already have. Keeping this in mind, your current Test engine should be insensitive to the application drivers and those are just plug&play “automation libraries” anyway.
Here is the Appium GitHub examples repo, which I like to use. You can find examples in:
RobotFramework
dotnet
java
node
perl
php
python
ruby

Run Selenium tests in one browser while using a second browser window

I am running Selenium automation test in one browser, but at the same time, I want to open the browser in another window and do something like checking mail, googling email then active mode or focus is coming to the current working window, not the automation test run browser.
Is it possible to work on the browser while automation test is run?
In general, when doing UI automation, you cannot use the test machine to do any other tasks that involve using the keyboard or mouse.
Since WebDriver automation performs keyboard and mouse input, such as typing text and clicking items, you will be constantly interfering by taking focus away from the WebDriver instance of the browser and doing your own mouse and keyboard interaction in other applications.
This will adversely affect both you and the automation, with neither being able to do what they want to do!
You should either use a separate test machine, or setup a virtual machine using software such as VirtualBox (free).
Did you try doing that?
Selenium uses WebDriver to communicate with a specific instance of a browser, not the currently focused window. So you should be able do continue to use other instances of browser windows. The best thing to do would be try.
If it isn't working, I would recommend getting a VM up and running and using that as your test environment. Generally that is the way I work to keep everything separate.
I ran my tests on Firefox and then used chrome on the side. Otherwise, run your tests on a remote machine.
You can do 2 things
1. Use a third tool to run test cases like Jenkins. so that test will run in memory.
2. If you are using firefox you can create a seperate firefox profile so that if you use firefox at the same time there should be any issue.
To Create new FF profile use below code:
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(false);
profile.setAssumeUntrustedCertificateIssuer(true);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, profile);
WebDriver driver = new FirefoxDriver(dc);

Selenium drivers don't remember user authentication

The application I'm testing requires a login with the user's Google account. Every time I log in, it displays/requires that I select the 'Allow Access' button as if it doesn't remember that I have already added it to my list of Authorized Access for my Google account. This doesn't happen when I test manually, only when I'm running Selenium. Has anyone come across an issue like this or know of a solution? Thanks in advance.
WebDriver driver = selenium_driver.get(); // using chrome driver
baseUrl = defaults.getProperty("base_url"); // this is set to my localhost
helper.ConnectToURL(baseUrl);
When this started happening, I had been using Selenium 2.28.0--since then, I've updated to 2.31.0 but it's exhibiting the same behavior.
Disclaimer: This is currently not possible according to the ChromeDriver wiki. It states in the "Known Issues" section "Cannot specify a custom profile". (https://code.google.com/p/selenium/wiki/ChromeDriver)
At some point when it is fixed, I would suggest creating or using the default chrome profile that has your authorized access set that your test uses whenever it starts up.
According to the ChromeDriver wiki: "By default, ChromeDriver will create a new temporary profile for each session".
Checkout this post for more in depth information regarding capabilities: http://code.google.com/p/chromedriver/wiki/CapabilitiesAndSwitches
I do my work in .NET and Windows; my set up would look something like this:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("start-maximized");
chromeOptions.AddArgument("user-data-dir=C:\\Users\\username\\AppData\\Local\\Google\\Chrome\\User Data\\Default");
capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability(ChromeOptions.Capability, chromeOptions);
ChromeDriver chromeDriver= new ChromeDriver(this.Environment.ChromeDriverLocation, chromeOptions);
If you are not limited to using Chrome for your tests you are able to create and use custom profiles using Firefox.