I am trying to get and access the elements from a pop-up/alert using selenium in java. Here is the code:
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Test {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "path_to_gecko_driver");
FirefoxDriver firefoxDriver = new FirefoxDriver();
firefoxDriver.get("some_url_which_needs_webcam_and_microphone");
WebElement element = new WebDriverWait(firefoxDriver, 20).until(ExpectedConditions.presenceOfElementLocated(new By.ByClassName("button_id")));
element.click();
JavascriptExecutor js = (JavascriptExecutor) firefoxDriver;
String title = String.valueOf(js.executeScript("return document.title"));
System.out.println("Title ==== " + title);
Object rezult = js.executeScript("return document.getElementById(\'nav-bar\')");
System.out.println("Browser element ==== " + rezult); //===>null
System.out.println("FINISH");
}
}
I am trying to access the browser pop-up elements, so I can choose between the available webcams or microphones and allow or deny the access. The JS code works just fine in the browser's console, but not in Java.
Any time I try to access any element in Java by using the JavascriptExecutor the element is null.
How can I access the pop-up elements by using the JavascriptExecutor in Java? Are there any other predefined configs or any other prerequisites that I am missing? Thank you
As you are invoking click() prior to this action we should induce WebDriverWait for the WebElement identified as getElementById('nav-bar') to render properly through ExpectedConditions.elementToBeClickable and try the following :
js.executeScript("return document.getElementById('nav-bar')");
Related
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
public class oo {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","D:\\Java\\Lib\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://google.com");
JavascriptExecutor js = (JavascriptExecutor) driver;
Object s = js.executeScript("return document.body.innerHTML;",null).toString();
System.out.println(s);
driver.close();
}
}
Above code returns nullPointerException.
Exception in thread "main" java.lang.NullPointerException at java.util.Arrays.stream(Unknown Source) at java.util.stream.Stream.of(Unknown Source) at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:484) at oo.main(oo.java:25)
When I remove optional object parameter, it goes compilation error.
Code:
JavascriptExecutor js = (JavascriptExecutor) driver;
Object s = js.executeScript("return document.body.innerHTML;").toString();
Error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method executeScript(String, Object[]) in the type JavascriptExecutor is not applicable for the arguments (String)
Using Selenium-server-standalone-3.141.59.jar
To extract and print the Page Source through JavascriptExecutor you can use the following (Java based) solution:
Syntax:
String page_source = ((JavascriptExecutor)driver).executeScript("return document.documentElement.innerHTML;").toString();
System.out.println(page_source);
Note: You need to induce a waiter for the page to load completely before extracting the Page Source.
package com.MavenLearning.Login;
import static org.testng.Assert.assertEquals;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class LoginOne {
#Test
public void LoginTestOne()
{
{
System.setProperty("webdriver.gecko.driver","C:\\Webdriver\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.demo.guru99.com/v4/");
driver.findElement(By.name("uid")).sendKeys("mngr105709");
driver.findElement(By.name("password")).sendKeys("jajeten");
driver.findElement(By.name("btnLogin")).click();
assertEquals(driver.getTitle(), "Guru99 Bank Manager HomePage");
String A = driver.getTitle();
System.out.println(A);
String B = "Guru99 Bank Manager HomePage";
System.out.println(B);
if (A.equals(B))
System.out.println("Page Title matches");
else
System.out.println("Page Title Doesn't Match");
}
}
}
Seniors, I have written my code in Selenium and stored it in a global method. I don't know how to call the method in the other class. When I was learning Selenium with Java I was easily calling methods to different class but stuck now with TestNG. I have tried importing the method package so everything should work but no success. Thanks in advance for help.
From Global if you mean public static then you just need to call it like below:
ClassName.functionName()
If it is not static then you need to create an object of that class and then call the function like:
MyClass my = new MyClass();
my.MyFunctionName();
Updated
You need to create a xml file and specifically all classes which you need to execute.
Example of xml
<?xml version="1.0" encoding="UTF-8"?>
<suite name="example suite 1" verbose="1" >
<test name="Regression suite 1" >
<classes>
<class name="com.first.example.demoOne"/>
<class name="com.first.example.demoTwo"/>
<class name="com.second.example.demoThree"/>
</classes>
</test>
</suite>
Source:
http://www.seleniumeasy.com/testng-tutorials/testngxml-example-to-execute-with-class-names
Video tutorial:
https://www.youtube.com/watch?v=cNhnqVWD_54
I am trying to execute the following program but getting a null pointer error. where #test is not able to access the object - driver. what could be the possible error i am making. Error -
JavaScript warning: https://www.google.com/xjs/_/js/k=xjs.s.en_US.MHBUsB8Me90.O/m=sx,c,sb,cdos,cr,elog,hsm,jsa,r,qsm,d,csi/am=wCJGjhccAPk_IRQStxAWyAImDiA/rt=j/d=1/t=zcms/rs=ACT90oGwMPBWhsFBKoM1svJAFUVoVQRQug, line 7: mutating the [[Prototype]] of an object will cause your code to run very slowly; instead create the object with the correct initial [[Prototype]] value using Object.create
https://www.google.com/?gws_rd=ssl
FAILED: websiteTitle
java.lang.NullPointerException
at myPackage.TestNGforHDFC.websiteTitle(TestNGforHDFC.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
package myPackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestNGforHDFC {
public WebDriver driver;
public String urlUnderTest = "http://www.google.com";
public String projLocation = "C:\\Users\\Nikita Agrawal\\Selenium\\geckodriver.exe";
#BeforeTest
public void login()
{
System.setProperty("webdriver.gecko.driver", projLocation);
WebDriver driver = new FirefoxDriver();
driver.get(urlUnderTest);
System.out.println(driver.getCurrentUrl());
}
#Test
public void websiteTitle()
{
System.out.println(driver.getTitle());
}
}
The class variable driver is never initialized, you're defining a new variable called driver inside the login method.
Use:
public void login(){
driver = new FirefoxDriver();
....
}
Instead of:
public void login(){
WebDriver driver = new FirefoxDriver();
....
}
Is there a way to run the same Selenium test case on many pages without specifically defining a list of pages?
Say, for example, I have a UIMap pageset defined like this:
var map = new UIMap();
map.addPageset({
name: 'pages',
description: 'all pages',
pathRegexp: '^thisistheroot/$'
});
In the pageset, I have all the elements defined for a test script that I want to test on each page in the pageset.
All of this is added to my core extensions.
Am I able to run a test case on the entire pageset? How can I do that?
I've looked into the issue a little more. Is there a way this is possible with Jenkins? https://jenkins-ci.org/
Edit:
I was trying to avoid using selenium webdriver, but if it is possible to obtain links as you would in a UIMap, that would probably point me in the right direction as well. I would try to iterate over the links with a single test case, which can easily be done in java. I'm using java for webdriver by the way.
Thanks.
The simple answer is "no" but Selenium WebDriver is one of the best choices for sure to find the links of a page and iterate over them. There is a very similar concept of your UIMapping called PageFactory where you map all the page elements in separate classes to keep the responsibility separate which makes debugging and refactoring much easier. I have used the PageFactory concept here.
Now coming back to your question, you can easily find the list of the links present in a page. In that case you just need to write the selector little carefully. You can then easily iterate over the links and come back and forth and so on.
Proof of concept on Google
BasePage
package google;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.NoSuchElementException;
/**
* Defines the generic methods/functions for PageObjects.
*/
public class BaseClass {
protected WebDriver driver;
/**
* #param _driver
* #param byKnownElement
*/
public BaseClass(WebDriver _driver, By byKnownElement) {
//assigning driver instance globally.
driver = _driver;
this.correctPageLoadedCheck(byKnownElement);
/* Instantiating all elements since this is super class
and inherited by each and every page object */
PageFactory.initElements(driver, this);
}
/**
* Verifies correct page was returned.
*
* #param by
*/
private void correctPageLoadedCheck(By by) {
try {
driver.findElement(by).isDisplayed();
} catch (NoSuchElementException ex) {
throw ex;
}
}
}
PageObject inherited BasePage
package google;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import java.util.List;
/**
* Created by Saifur on 5/30/2015.
*/
public class GoogleLandingPage extends BaseClass {
private static final By byKnownElement = By.xpath("//a[text()='Sign in']");
/**
* #param _driver
*/
public GoogleLandingPage(WebDriver _driver) {
super(_driver, byKnownElement);
}
//This should find all the links of the page
//You need to write the selector such a way
// so that it will grab all intended links.
#FindBy(how = How.CSS,using = ".gb_e.gb_0c.gb_r.gb_Zc.gb_3c.gb_oa>div:first-child a")
public List<WebElement> ListOfLinks;
}
BaseTest
package tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
public class BaseTest {
public WebDriver driver;
String url = "https://www.google.com/";
#BeforeClass
public void SetUpTests() {
driver = new FirefoxDriver();
//Navigate to url
driver.navigate().to(url);
//Maximize the browser window
driver.manage().window().maximize();
}
#AfterClass
public void CleanUpDriver() throws Exception {
try {
driver.quit();
}catch (Exception ex){
throw ex;
}
}
}
Link Iterator test inheriting BaseTest
package tests;
import google.GoogleLandingPage;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;
import java.util.List;
/**
* Created by Saifur on 5/30/2015.
*/
public class LinksIteratorTests extends BaseTest {
#Test
public void IterateOverLinks(){
GoogleLandingPage google = new GoogleLandingPage(driver);
List<WebElement> elementList = google.ListOfLinks;
for (int i=0;i<elementList.size(); i++){
elementList.get(i).click();
//possibly do something else to go back to the previous page
driver.navigate().back();
}
}
}
Note: I am using TestNG to maintain the tests and please note for lazy loading page you may need to add some explicit wait if necessary
Actually it's simple to run an IDE test against 1 specific page (base url actually): java -jar selenium-server.jar -htmlSuite "*firefox" "http://baseURL.com" "mytestsuite.html" "results.html"
So what you need to do is use jenkins (or any bash/batch script) to run that command multiple times with the base url set as "http://baseURL.com/page1", "http://baseURL.com/page2", etc.
This will only get you as far as static list of pages to test against. If you want a dynamic list you'd have to also "crawl" the pages and you could do that in the similar batch/bash script to obtain the list of pages to test against.
In this case you'd best be investing beyond selenium IDE and switch to webdriver where you'll have more power to loop and flow control.
in this code Android app opens a web page with WebView and extracts a text from HTML which is between tags "body" and "/body".
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
public class MainAc extends Activity {
/** Called when the activity is first created. */
#SuppressLint({ "JavascriptInterface", "SetJavaScriptEnabled" })
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView = (WebView) findViewById(R.id.web);
TextView text2 = (TextView) findViewById(R.id.text);
Button infoButton = (Button) findViewById(R.id.b1);
infoButton.setOnClickListener(new OnClickListener(){
public void onClick(View view){
// here is your button click logic, for example running another activity (page)
startActivity(new Intent(MainAc.this, JavaInterface.class));
}
});
class Javasc {
private TextView t2;
public Javasc (TextView i)
{
t2 = i;
}
#SuppressWarnings("unused")
public void processContent(String ii)
{
final String content = ii;
t2.post(new Runnable()
{
public void run()
{
t2.setText(content);
}
});
}
}
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new Javasc(text2), "INTERFACE");
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url)
{
view.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByTagName('body')[0].innerText);");
}
});
webView.loadUrl("http://www.nytimes.com/2014/08/03/sports/basketball/pacers-paul-george-has-surgery-after-badly-injuring-leg.html?ref=sports");
}
}
Is it possible to use JavaScript functions for extracted text in android's TextView ?
for example this JavaScript function (or it could be any other JS function where need to work with text)
function myFunction() {
var text = document.body.innerText;
var titles =text.match(/^\n(.+?)\n\n/mg);
for (var i = 0; i < titles.length; i++) {
document.write(titles[i] + "<br />" + "<br />");
}
}
Thanks for answers :)
According to this article, the Dalvik VM supports Java's scripting features (javax.script). One of the premier languages supported by the javax.script stuff is, unsurprisingly, JavaScript.
So in theory, you can use the javax.script stuff to execute JavaScript code and get back results. I think (also based on that article), that you have to include the relevant jar(s) (javax.script isn't in the Android SDK). Fortunately, though, javax.script is largely a set of interfaces, which are implemented by jars for specific scripting languages.
Some resources about using javax.script to run script code:
The Java Scripting API (Oracle)
Java Scripting Programmer's Guide (Oracle)
You can use Rhino to achieve this. See specifically Context.jsToJava.
https://vec.io/posts/embed-javascript-in-android-java-code-with-rhino
You want to do a regex on a text and resorting to javascript for it, which is unnecessary when you have a better faster API in java with no overhead.
The equivalent to your example is:
http://developer.android.com/reference/java/util/regex/Pattern.html
Pattern p = Pattern.compile("/^\n(.+?)\n\n/mg");
Matcher m = p.matcher(myTextView.getText());
while (m.find()) {
String titles = m.group(1);
Log.V("TAG", titles);
}
There are plenty of other text analysis solutions included in the core framework or external libraries that have been proved for both mobile and server. You just have to look it up and not resort to a worse API you're comfortable with.