How disable debuger on chrome webdriver in Java? - java

My target application use <script type="text/javascript">debugger;</script> So my page is blocked by break point.
How disable debugger on chrome webdriver in Java?
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import com.github.noraui.utils.Utilities.OperatingSystem;
import com.github.noraui.utils.Utilities.SystemArchitecture;
public class Bot {
public static void main(String[] args) {
final OperatingSystem currentOperatingSystem = OperatingSystem.getCurrentOperatingSystem();
String pathWebdriver = String.format("src/test/resources/drivers/%s/googlechrome/%s/chromedriver%s", currentOperatingSystem.getOperatingSystemDir(),
SystemArchitecture.getCurrentSystemArchitecture().getSystemArchitectureName(), currentOperatingSystem.getSuffixBinary());
System.setProperty("webdriver.chrome.driver", pathWebdriver);
ChromeOptions options = new ChromeOptions();
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://rd.huangpuqu.sh.cn/website/html/shprd/shprd_tpxw/List/list_0.htm");
}
}
I think, add javascipt code (revert of debugger;) by this:
((JavascriptExecutor) driver).executeScript("...");
EDIT WITH HEADLESS MODE + SCREENSHOT:
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import com.github.noraui.utils.Utilities.OperatingSystem;
import com.github.noraui.utils.Utilities.SystemArchitecture;
public class Bot {
public static void main(String[] args) throws IOException {
final OperatingSystem currentOperatingSystem = OperatingSystem.getCurrentOperatingSystem();
String pathWebdriver = String.format("src/test/resources/drivers/%s/googlechrome/%s/chromedriver%s", currentOperatingSystem.getOperatingSystemDir(),
SystemArchitecture.getCurrentSystemArchitecture().getSystemArchitectureName(), currentOperatingSystem.getSuffixBinary());
System.setProperty("webdriver.chrome.driver", pathWebdriver);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);
driver.get("http://rd.huangpuqu.sh.cn/website/html/shprd/shprd_tpxw/List/list_0.htm");
final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
FileUtils.forceMkdir(new File(System.getProperty("user.dir") + File.separator + "downloadFiles"));
FileUtils.writeByteArrayToFile(new File(System.getProperty("user.dir") + File.separator + "downloadFiles" + File.separator + "bot.jpg"), screenshot);
}
}
The result is same, my sceenshot is white.

Related

Cannot get Java Selenium in headless mode

I've searched around for awhile now and it seems like i just need to add chrome options to get selenium to go into headless mode. I am quite new and i maybe missing something but i've tried the adding the code below into my scripts in to various areas and it doesn't seem to work.
ChromeOptions options = new ChromeOptions();
options.addArguments("window-size=1400,800");
options.addArguments("headless");
I've tried the following
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.openqa.selenium.chrome.ChromeOptions;
public class TestPlan {
private static final WebDriver driver = new ChromeDriver();
#BeforeSuite
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", Utils.CHROME_DRIVER_LOCATION);
ChromeOptions options = new ChromeOptions();
options.addArguments("window-size=1400,800");
options.addArguments("headless");
WebDriver driver = new ChromeDriver(options);
WebDriverWait wait = new WebDriverWait(driver, 30);
}
#Test(testName = "Login Page")
public static void wikiLogin() {
driver.get(Utils.WIKI_LOGIN_URL);
WikiLogin wikilogin = new WikiLogin(driver);
wikilogin.loginButton();
}
and
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import org.openqa.selenium.chrome.ChromeOptions;
public class TestPlan {
private static final WebDriver driver = new ChromeDriver();
#BeforeSuite
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", Utils.CHROME_DRIVER_LOCATION);
WebDriverWait wait = new WebDriverWait(driver, 30);
}
#Test(testName = "Login Page")
public static void wikiLogin() {
ChromeOptions options = new ChromeOptions();
options.addArguments("window-size=1400,800");
options.addArguments("headless");
WebDriver driver = new ChromeDriver(options);
driver.get(Utils.WIKI_LOGIN_URL);
WikiLogin wikilogin = new WikiLogin(driver);
wikilogin.loginButton();
}
With no success. Would appreciate some help. Thanks!

Taking full page screen shot of really long webpages using Ashot, Selenium Java Client, ChromeDriver

the URL i am trying to take a screenshot is https://www.smashingmagazine.com/2017/05/long-scrolling/
Attached is the screenshot obtained from Ashot. [screenshot from ashot] - https://i.stack.imgur.com/CdeYf.jpg (notice it is incomplete)
I feel like its a memory issue and not a bug. Any pointers would be appreciated.
This is the code I am using to take screenshots.
import java.io.File;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class ashot_CompletePage {
public static void main(String[] args) throws Exception {
System.setProperty("god.bless.you", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.smashingmagazine.com/2017/05/long-scrolling/");
new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("jQuery"));
Screenshot myScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver);
ImageIO.write(myScreenshot.getImage(),"PNG",new File("./Screenshots/elementScreenshot.png"));
driver.quit();
}
}

Trying to accept location permission pop up with Appium/Android 7/Java

I am using Appium 1.6.3 and a Google Pixel with Android 7.1.1.
I am trying to load the Waze app which is already installed on the phone and accept the initial location permission request.
I have tried the following:
capabilities.setCapability("autoGrantPermissions", "true");
Also:
capabilities.SetCapability("autoAcceptAlerts", true);
But these doesn't seem to do the trick.
I have read on other sites, that the auto granting of permission is only available on iOS?
The code I have currently:
import io.appium.java_client.MobileBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.html5.Location;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
public class mapTests {
AndroidDriver driver;
Dimension size;
String destDir;
DateFormat dateFormat;
#BeforeTest
public void setup() throws Exception{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "FA6C90301474");
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "7.1.1");
capabilities.setCapability("appPackage", "com.waze");
capabilities.setCapability("appActivity", "com.waze.FreeMapAppActivity");
capabilities.setCapability("noSign", true);
capabilities.setCapability("autoGrantPermissions", "true");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void testOne()throws Exception {
Location location = new Location(53.7775174,-1.800881200000049,224);
driver.setLocation(location);
takeScreenShot();
}
public void takeScreenShot() {
destDir = "screenshots";
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
dateFormat = new SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");
new File(destDir).mkdirs();
String destFile = dateFormat.format(new Date()) + ".png";
try {
FileUtils.copyFile(scrFile, new File(destDir + "/" + destFile));
} catch (IOException e) {
e.printStackTrace();
}
}
#AfterTest
public void tearDown() throws Exception{
driver.quit();
}
}
Thank you!
Actually, I have found an solution to my problem:
WebElement allow_location = driver.findElement(By.id("com.android.packageinstaller:id/permission_allow_button"));
allow_location.click();
Thread.sleep(1000);
This works a charm!
Try to use:
driver.switchTo().alert().accept();
or
driver.switchTo().alert().dismiss();
And don't forget to wait until the alert is present and then click on it.
This capabilities worked for me:
caps = {}
caps[“platformName”] = "Android"
caps[“platformVersion”] = "7.0"
caps[“deviceName”] = "Pixel_C_API_24"
caps[“automationName”] = "UiAutomator2"
caps[“app”] = "/home/tests/app-debug.apk"
caps[“noReset”] = False
caps[“autoGrantPermissions”] = True
caps[“appPackage”] = "com.mypackage"
caps[“appActivity”] = “.MyActivity”
Regards.
try to use the AndroidMobileCapabilityType.It will enable all permissions that apk requires.
capabilities.setCapability(AndroidMobileCapabilityType.AUTO_GRANT_PERMISSIONS, "true");

Where to find chromedriver.log in selenium using java. Where can i see the log file of chromedriver?

I want to extract chrome logs in java selenium web driver project. I am using following to launch chromedriver.
> dc = DesiredCapabilities.chrome();
> System.setProperty("webdriver.chrome.logFile","//src//resource//log.txt");
Another similar question has following solution.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class ChromeConsoleLogging {
private WebDriver driver;
#BeforeMethod
public void setUp() {
System.setProperty("webdriver.chrome.driver", "c:\\path\\to\\chromedriver.exe");
DesiredCapabilities caps = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
driver = new ChromeDriver(caps);
}
#AfterMethod
public void tearDown() {
driver.quit();
}
public void analyzeLog() {
LogEntries logEntries = driver.manage().logs().get(LogType.BROWSER);
for (LogEntry entry : logEntries) {
System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());
//do something useful with the data
}
}
#Test
public void testMethod() {
driver.get("http://mypage.com");
//do something on page
analyzeLog();
}
}
Solution is not useful for me, as I want to save browser log for every session with the session Id itself.

How to connect phantomjs to selenium using java

I have a problem with my code - see below. Could somebody tell me what is wrong? It won't connect but everything is correct.
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.testng.annotations.Test;
public class Main {
#Test
public static void main(String[] args){
File src = new File("phan//bin//phantomjs");
// System.out.println("test:" + File);
System.setProperty("phantomjs.binary.path",src.getAbsolutePath());
WebDriver driver = new PhantomJSDriver();
driver.get("http://facebook.com");
System.out.println(driver.getTitle());
}
}
Try following:
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
...
File phantomJSBinary = new File("path" + File.separator + "to" + File.separator + "phantomjs");
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomJSBinary.getAbsolutePath());
WebDriver driver = new PhantomJSDriver(caps);
...

Categories