I am using java + selenium webdriver for webautomaiton.
For safari browser 10.1 version, I need the browser to be full screen before test started.However
driver.manage().window().maximize();
does not work
I tried few options but no luck.
1.
seems no option available for doing something like which would write in the plist file of /Library/Preferences folder of mac
defaults write com.apple.Safari
2
WebElement element = Wait.wait.until(visibilityOfElementLocated(By.cssSelector(".logo-large")));
element.sendKeys(Keys.CONTROL , Keys.COMMAND , "f");
element.sendKeys(Keys.CONTROL , Keys.COMMAND , "F");
Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL).keyDown(Keys.COMMAND).sendKeys("F").perform();
action.keyDown(Keys.CONTROL).keyDown(Keys.COMMAND).sendKeys("f").perform();
Is there anyway I can do it using send keys, or write in the plist file or through javascript.
Try below :-
public static void maximizeScreen(WebDriver driver) {
java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point position = new Point(0, 0);
driver.manage().window().setPosition(position);
Dimension maximizedScreenSize =
new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
driver.manage().window().setSize(maximizedScreenSize);
}
Hore it will help you
Take a look here (Native Fullscreen JavaScript API): http://johndyer.name/native-fullscreen-javascript-api-plus-jquery-plugin/
Sample:http://johndyer.name/lab/fullscreenapi/
You have to update the safari browser, update the browser with Safari 11+ version
then you are able to maximize it using this code:
driver.manage().window().maximize();
Related
My task:
1)I have to go this url : https://www1.nseindia.com/live_market/dynaContent/live_watch/pre_open_market/pre_open_market.htm
2)Select the option "FO Stocks" from selection box so that New table of FO Stocks will appear.
3)After that Sort the table in decending by click on "FFM Column" which has href table header.
Extract Top 5 Rows of the Table.
My Approach:
Selenium Webdriver using Java
1)I have sucessfully opened the url in FireFox tried with Chrome which is talking to long to load the whole page.
2)After Loading of webpage I changed the Selection Box Value to FO by using ID of select box and value of Option. the following code Snippet shows this.
Select dropdown = new Select(driver.findElement(By.id("selId")));
dropdown.selectByValue("fo");
Problem: The table doesn't update value according to the selected option might be because onchange() function of javascript doesn't execute/enabled after changing value of selection box on that page.
3)How to click on this Column using Selenium Webdriver (Any tips is Appreciated).
Here is the Code
public class FirstSeleniumTest {
WebDriver driver;
public void waitForLoad(WebDriver driver) {
System.out.print("waiting for javascript sucessfully");
new WebDriverWait(driver, 120).until((ExpectedCondition<Boolean>) wd ->
((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete"));
}
public void launchBrowser() {
// System.setProperty("webdriver.chrome.driver", "C:\\Downloads\\chromedriver_win32\\chromedriver.exe");
// driver = new ChromeDriver();
System.setProperty("webdriver.gecko.driver","C:\\Downloads\\geckodriver-v0.26.0-win64\\geckodriver.exe");
DesiredCapabilities dcap = new DesiredCapabilities();
dcap.setCapability("pageLoadStrategy", "eager");
FirefoxOptions opt=new FirefoxOptions();
opt.merge(dcap);
WebDriver driver = new FirefoxDriver(opt);
System.out.print("Driver loaded sucessfully");
driver.get("https://www1.nseindia.com/live_market/dynaContent/live_watch/pre_open_market/pre_open_market.htm");
waitForLoad(driver);
Select dropdown = new Select(driver.findElement(By.id("selId")));
waitForLoad(driver);
dropdown.selectByValue("fo");
waitForLoad(driver);
}
public static void main(String[] args)
{
FirstSeleniumTest ft=new FirstSeleniumTest();
ft.launchBrowser();
}
}
Instead of selecting, trying removing the attribute of the first element and add "selected" attribute to the option you want to select through jquery/javascript. After that trigger the element's on change method.
Try triggering that with:
$('selId').trigger('change')
Follow the below steps which have worked for me
Launch the chrome manually from custom user directory as shown in image
Go to chrome browser exe path
in windows will be like C:\Program Files (x86)\Google\Chrome\Application\
Open CMD in this path and execute the below command
chrome.exe --user-data-dir="C:\selenum\AutomationProfile" where C:\selenum\AutomationProfile is custom user data directory
Now launch manually your URL which is to be automated in this browser
Wait until webpage loads completely and close the browser
Now from selenium launch the browser from this custom user-data-dir using the code below
public class Test {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "Your Driver path");
ChromeOptions option=new ChromeOptions();
//arguments to launch chrome with Custom user dir path
option.addArguments("--user-data-dir=C:\\selenum\\AutomationProfile\\");
WebDriver driver = new ChromeDriver(option);
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.get("https://www1.nseindia.com/live_market/dynaContent/live_watch/pre_open_market/pre_open_market.htm"); // WebElement
Select dropdown = new Select(driver.findElement(By.id("selId")));
dropdown.selectByValue("fo");
Thread.sleep(2000);
dropdown.selectByValue("sme");
Thread.sleep(2000);
dropdown.selectByValue("niftybank");
Thread.sleep(2000);
System.out.println(driver.getTitle());
}
}
Handle dropdowns with required webdriver wait to scrape the table data and using Thread.sleep is not recommended one.
Reason behind launching chrome in custom user profile is to ensure fast website loading in chrome and also website is blocking the client browser when it is launched from selenium that's the reason it takes time to load in chrome automated browser and no data change based on the dropdown values
Hope this will be helpful
I am using appium and java to automate my app. I need to scroll in a page till a specific text/Element. I am struggling since two days and have tried many solutions but it dint work. Listing down all the solutions and scenarios:
Appium version: 1.4.0
Java-client version : 4.1.2
Driver: RemoteWebDriver
Solution1.1:
Tried with Java Script. Here is the code:
JavascriptExecutor js = (JavascriptExecutor)driver;
HashMap scrollObjects = new HashMap();
scrollObjects.put("direction", "down");
scrollObjects.put("text", "Yes");
js.executeScript("mobile: swipe", scrollObjects)
Solution 1.2:
WebElement wb = driver.findElement(By.xpath("//*[#resource-id = 'com.practo.fabric:id/toolbar']/following-sibling::android.widget.LinearLayout"));
JavascriptExecutor js = (JavascriptExecutor)driver;
HashMap scrollObjects = new HashMap();
scrollObjects.put("direction", "down");
scrollObjects.put("text", "Yes");
scrollObjects.put("element",wb);
js.executeScript("mobile: swipe", scrollObjects);
Problem here is i dont have list view. I have linear layout so have tried with providing linear layout xpath also once but with no luck.
Solution2:
org.openqa.selenium.Dimension size =driver.manage().window().getSize();
int starty = (int) (size.height * 0.80);
int endy = (int) (size.height * 0.20);
int startx = size.width / 2;
driver.swipe(startx, starty, startx, endy, 3000);
System.out.println("swiping is done ");
Here driver.swipe is giving error as i am RemoteWebDriver. When i try to use AndroidDriver it says deprecated.
what should i do in this situation?
You are facing issue because of the appium server and java client library version mismatch. Kindly update appium server to 1.5/1.6 else you can use java-client library version 2.1.0.
If you choose to use java-client2.1.0 with appium 1.4 we are having a method named "scrollTo" which scrolls to a particular text. I have also used "swipe" as you mentioned in solution 2 in this combo.
driver.scrollTo("Like"); //Scrolls to Like text in the app
I got it solved by using AppiumDriver from java-client. As i am using latest java-client version (5.0.0-BETA6) it says "swipe" methos is deprecated. but it works.
driver.swipe(300, 701, 300, 441, 3000);
solved it using TouchAction class also.
TouchAction action = new TouchAction(driver);
action.press(300, 701).waitAction(2000).moveTo(300, 441).release();
action.perform();
Note: coordinates are hard coded.
I want to launch chrome browser with viewport size of some resolution , for example 800x600 , I tried following code
var width = 800;
var height = 600;
driver.manage().window().setSize(width, height);
Above code just resizing window to 800x600 , when I run window.screen.width in developer console it always return 1440
I even tried to add chrome option with --window-size=x,y option
var o = new chrome.Options();
o.addArguments("--window-size=640,480");
driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).setChromeOptions(o).build();
Above code still do the same ,just resizing the window.
Some one please tell me any chrome command line option to set default viewport like we do in chrome developer console (as shown below) , or please tell any option to do directly in selenium webdriverJS.
The code you are providing is working perfectly fine.
However, you are checking the resolution of the screen, not the size of the window! This is what window.screen.width and window.screen.height refer to. To retrieve the size of the window, use window.innerWidth (and innerHeight). The following works for me.
var webdriver = require('selenium-webdriver'),
driver = null;
function logWindowSize() {
driver.executeScript(function() {
return [window.innerWidth, window.innerHeight];
}).then(console.log);
}
// construct driver
driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.build();
driver.get('http://www.google.com');
// resize window
logWindowSize();
driver.manage().window().setSize(640, 480);
logWindowSize();
driver.quit();
This will log the following to my console.
[ 945, 1018 ]
[ 640, 375 ]
Note that the height of the window is different from what was set. This is because of the height of the tab bar and navigation bar. You can set the inner window size by first setting it to an arbitrary value, checking the difference and then setting it to what you want plus that difference. like so.
var webdriver = require('selenium-webdriver'),
driver = null,
sizeWidth = 640,
sizeHeight = 480;
// construct driver
driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.build();
driver.get('http://www.google.com');
// resize window
driver.manage().window().setSize(sizeWidth, sizeHeight);
driver.executeScript(function() {
return [window.innerWidth, window.innerHeight];
}).then(function(actualSize) {
driver.manage().window().setSize(
2 * sizeWidth - actualSize[0],
2 * sizeHeight - actualSize[1]
);
// the following will log the sizes we want
driver.executeScript(function() {
return [window.innerWidth, window.innerHeight];
}).then(console.log);
});
driver.quit();
The problem
It seems that you really want to influence what window.screen.width and window.screen.height return. This is normally not possible, except that these values change when using the device mode (and toolbar) in Chrome. So, let's trigger those, right?
// open inspector and responsive design mode
driver.actions()
.keyDown(Key.CONTROL)
.keyDown(Key.SHIFT)
.sendKeys('im')
.keyUp(Key.SHIFT)
.keyUp(Key.CONTROL)
.perform();
Unfortunately, this won't work in Chrome. To quote that answer:
The Chrome driver uses the Chrome remote debugging protocol to communicate with the browser. This is the same protocol that the developer console uses also. Unfortunately, Chrome is designed so that only one client can be attached using the protocol at a time, so that means either the developer tools, or the driver, but not both simultaneously. — JimEvans
Bummer. It looks like this can be made to work using Firefox, however. Unfortunately, it seems that performing actions is currently not possible using Selenium + geckodriver, at the moment. It is also not possible to open this mode using JavaScript. We can however send keys to an element.
A solution
The following works for me.
var webdriver = require('selenium-webdriver'),
firefox = require('selenium-webdriver/firefox'),
By = webdriver.By,
Key = webdriver.Key,
driver = null;
// construct driver
var profile = new firefox.Profile(),
devicePreset = [{
width: 640,
height: 480,
key: '640x480',
name: 'Mobile Device'
}];
profile.setPreference('devtools.responsiveUI.presets',
JSON.stringify(devicePreset));
var opts = new firefox.Options();
opts.setProfile(profile);
var builder = new webdriver.Builder().forBrowser('firefox');
builder.setFirefoxOptions(opts);
driver = builder.build();
driver.get('http://www.google.com');
// open responsive design mode
driver.findElement(By.css('input[name="q"]'))
.sendKeys(Key.chord(Key.CONTROL, Key.SHIFT, 'm'));
driver.get('https://www.iplocation.net/find-ip-address');
Note that I set a preference for Firefox that indicates the size to use in the responsive design mode. You can change this to whatever screen size you prefer.
I am trying to work out a comparable command to use in jmeter webdriver sampler (JavaScript) how to do a waitForPopUp command. There must be a way. I have something that works for waiting for an element, but I can't work it out for a popup.
Update
I am using this code for waiting for an element:
var wait = new support_ui.WebDriverWait(WDS.browser, 5000)
WaitForLogo = function() {
var logo = WDS.browser.findElement(org.openqa.selenium.By.xpath("//img[#src='/images/power/ndpowered.gif']"))
}
wait.until(new com.google.common.base.Function(WaitForLogo))
And this works, but I can't work out how reuse this to wait for a popup, that has no name, in Java I have used:
selenium.waitForPopUp("_blank", "30000");
selenium.selectWindow("_blank");
And that works, but I can't work out an comparable JavaScript that will work in Jmeter for performance, as I can't get Java working in Jmeter.
I was able to get this working using:
var sui = JavaImporter(org.openqa.selenium.support.ui)
and:
wait.until(sui.ExpectedConditions.numberOfWindowsToBe(2))
In WebDriver Sampler you have the following methods:
WDS.browser.switchTo.frame('frame name or handle') - for switching to a frame
WDS.browser.switchTo.window('window name or handle') - for switching to a window
WDS.browser.switchTo.alert() - for switching to a modal dialog
WDS.browser.getWindowHandles() - for getting all open browser window handles
See JavaDoc on WebDriver.switchTo method and The WebDriver Sampler: Your Top 10 Questions Answered guide for more details.
With the below code block it opens a chrome browser fine it just won't full screen the browser using F11. i used to use C# and selenium and that worked fine using this method on chrome and different browsers. It finds the element 'body' but then does not send the key press. Am I doing something wrong here that i should be requiring some other library?
the documentation for webdriverjs is pathetic and there is very few examples, I am seriously considering dumping it for something else possibly python.
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
driver.get('https://www.google.co.uk/');
driver.wait(function () {
return driver.getTitle().then(function (title) {
return title === 'Google';
});
}, 1000);
driver.findElement(webdriver.By.xpath('/html/body')).sendKeys("F11");
why are we doing this. we are developing a website that will change depending on size 800x600 + with and without the toolbar depending on how the screen is used different items will be displayed. i can maximise the window using,
driver.manage().window().maximize();
This however still leaves the toolbar present and doesn't act as if the user has pressed the F11 key.
it tooks some time to find it but you should have all the Keys in webdriver.Key
driver.findElement(webdriver.By.xpath('/html/body')).sendKeys(webdriver.Key.F11);
Hope it helps!
A co-worker has just discovered that it works well in C# with:
Driver.Instance.Manage().Window.FullScreen();
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).send_keys(Keys.F11).perform()
I use a similar command to toggle JS via Firefox's NoScript add-on
edit: I just tested, and it works!
This should work for you:
driver.findElement(webdriver.By.xpath('/html/body')).sendKeys(Keys.F11);
You can maximise the window using:
driver.manage().window().maximize();
For me the one emulating correctly the F11 on startup is:
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-fullscreen");
WebDriver driver = new ChromeDriver(options);
Or if the kiosk mode is preferred:
ChromeOptions options = new ChromeOptions();
options.addArguments("--kiosk");
WebDriver driver = new ChromeDriver(options);