When I am trying to open a site through TestNG, a security page is coming and for that I have click a link text "Continue to this website(not recommended)." that i have done through TestNG coding but it is giving java.lang.NullPointerException error.
and this is the code which i am trying......
public class Registration {
WebDriver driver;
WebElement element;
WebElement element2;
WebDriverWait waiter;
#Test(priority = 1)
public void register_With_Cash() throws RowsExceededException, BiffException, WriteException, IOException
{
File file = new File("D:\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver=new InternetExplorerDriver();
driver.get("https://172.25.155.250/loginpage.aspx");
sleep(10000);
waiter.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Continue to this website (not recommended).")));
driver.findElement(By.linkText("Continue to this website (not recommended).")).click();
sleep(50000);
Thanx in advance for any kind of help.
you can use the below javascript to click the Continue to the Website(not recommended) link.
driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()");
Related
I've been spending more than an hour to find similar cases but nothing helpful. I am new to Java and Selenium and only one member who develops the Selenium Automation script at the moment.
Okay, the target system's HTML code structure is as below.
What I did to click on the element on the topframe is, for example, (simplified snippet);
Page page = new Page();
page.setTopFrame();
page.a_button.click();
class Page{
#FindBy(id="a_button")
public WebElement a_button;
public void setTopFrame() {
driver.switchTo().frame("topframe");
}
}
But when I run this codes it returns "org.openqa.selenium.NoSuchElementException" Exception.
I don't know what I am missing now. Any advice will be thankful.
--Edited--
Since I post this one, other Exception found which is "NullPointerException: Cannot invoke "org.openqa.selenium.SearchContext.findElement(org.openqa.selenium.By)" because "this.searchContext" is null.
The constructor in Page class should initialised with driver from main Test class. Be like.
Page page = new Page(driver);
And Page class has
class Page{
WebDriver driver;
public Page (WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver, this);
}
...
}
Maybe you are currently in another frame. So try:
public void setTopFrame() {
driver.switchTo().defaultContent();
driver.switchTo().frame("topframe");
}
Page class{
WebDriver driver;
public Page (WebDriver driver){
this.driver = driver;
PageFactory.initElements(driver, this);
}
...
}
Thank you all read my question.
I hope this answer help someone in trouble with same issue.
I have a couple issues when I run the test with edge web browser.
The first issue is that every time I run the test by run as > testng test , the automation will close all the existing open edge web browser and then it will open a brand new edge browser but it is blank (could not load the url)
The second issue is that the automation would only load the url if I manually close all of the existing edge browser.
Is there a way to fix these issues? please help me thank you in advance.
Here is my code
public class OpenThePageUsingEdge {
public String baseUrl = "https://catalog-qa.baylorgenetics.com/search";
String driverPath = "C:\\Eclipse\\MicrosoftWebDriver.exe";
public WebDriver driver ;
//initiate NameofInsured as the GenerateData class
//GenerateData NameOfAddrecipients;
#BeforeTest
public void setup(ITestContext ctx) {
TestRunner runner = (TestRunner) ctx;
runner.setOutputDirectory("J:\\zzQA Selenium Automation Suite\\Test Results");
}
#Test
public void openThePageusingEdge () throws InterruptedException {
System.out.println("launching Edge browser");
System.setProperty("webdriver.edge.driver", driverPath);
driver = new EdgeDriver();
driver.get(baseUrl);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
I am trying to upload the file in selenium using java. When I run my test case then it passed every time but the file is not uploading. See below site and my code where I performing.
Site URL: https://files.fm/
Xpath where i want to upload: //input[#name='file_upload[]']
Note: If this xpath is incorrect then please update in comment.
Code:
#BeforeTest
public void OpenBrowser() {
System.setProperty("webdriver.chrome.driver", "./driver/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://files.fm/");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
#Test
public void FileUpload() throws InterruptedException, IOException {
Thread.sleep(2000);
WebElement file = driver.findElement(By.xpath("//input[#name='file_upload[]']"));
file.sendKeys("C:/Users/Admin/Pictures/Lighthouse.jpg");
}
use below code :
WebElement file = driver.findElement(By.xpath("//input[#id='file_upload']//following-sibling::input"));
file.sendKeys("C:/Users/Admin/Pictures/Lighthouse.jpg");
WebElement startUploadButton = driver.findElement(By.xpath("//div[#id='savefiles']//div"));
startUploadButton.click();
Hope that helps you:)
You choosed the wrong input. I tried to send to the other input and it worked
Here is xpath of it.
String inPath = "//*[#id='uploadifive-file_upload']/input[2]";
I have problem with testing my app which is intended for online shopping. I noticed that all buttons from application working but I have problem with system buttons(look at the picture). I’m using to recognize the button UiAutomatorViewer.
At the picture I’m trying add product to cart but when I’m click button “Dodaj”(means Add) the window closes but there are any actions in application, the product should be added to cart. I tested this application manually and everything work correct.
I tried to operate the button in many ways but still nothing. Does anyone know how to solve it? It’s very important functions to testing for me.
public class Product extends MainPage {
private AndroidDriver<AndroidElement> driver;
#FindBy(id = "com.mec.leroy:id/product_add_to_cart")
private WebElement addToCart;
#FindBy(xpath = "//android.widget.Button[#index='1']") //problem with button
private WebElement buttonAdd;
#FindBy(id = "com.mec.leroy:id/product_name")
private WebElement productName;
public Product(AndroidDriver driver) {
super(driver);
this.driver = driver;
}
public Product addProduct() throws InterruptedException {
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"do koszyka\"));");
addToCart.click();
buttonAdd.click();
Thread.sleep(5000);
return new Product(driver);
}
public boolean productName() throws InterruptedException {
Thread.sleep(2000);
try {
productName.getText();
return true;
} catch (ElementNotVisibleException e) {
return false;
}
}
public class Connector {
public AndroidDriver Settings() throws IOException, InterruptedException {
runAppium();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "33004db396a6c2d1"); // nazwa urządzenia
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UiAutomator2");
capabilities.setCapability("noRest", "true");
capabilities.setCapability("fullReset", "false");
// capabilities.setCapability("useKeystore", "true");
//uruchomienie aplikacji z poziomu telefonu
capabilities.setCapability("appPackage", "com.mec.leroy");
capabilities.setCapability("appActivity", "com.mec.leroy.main.activity.MainActivity");
//inicjalizacja połączenia z Appium Server
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL("http://0.0.0.0:4723/wd/hub"), capabilities);
return driver;
}
You are using UiAutomatorViewer (that uses old UiAutomator), but in your tests you are providing UiAutomator2: important to know that these frameworks build snapshot xml in a different way, so your locator might be incorrect.
I suggest to try inspect and interact with your button using official appium-desktop:
Install & launch it
Run the server via appium-desktop
Set exact the same capabilities you have in the code, create new session
Now you can inspect your elements and try to click your button from inspector.
I have a button with the follow html:
<span ng-show="!IsGoogleLinked()" aria-hidden="false" class="">Link Google</span>
<span ng-show="IsGoogleLinked()" aria-hidden="true" class="ng-hide">UnLink Google</span>
When you click this button and it goes through the linking process the button changes text from "Link Google" to "Unlink Google". I want to use the webdriver in such a way that'll check if the text in the button is "Unlink Google" it will click the button, and I'm missing a piece I hope someone could provide.
Here is the updated version using individual browser selection:
#When("^I check if the user has a \"([^\"]*)\" linked account$")
public void linkChecker(String socialLoginXpathNotLinked) throws Throwable {
driverWait = new WebDriverWait(driver, 20);
driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(socialLoginXpathNotLinked)));
driverElement = driver.findElement(By.xpath(socialLoginXpathNotLinked));
if(!driverElement.isDisplayed())
{
isLinked = true;
}
}
#Then("^I unlink user if \"([^\"]*)\" linked$")
public void unlinkUser(String socialLoginXpathLinked) throws Throwable {
if(isLinked)
{
driver.findElement(By.xpath(socialLoginXpathLinked)).click();
isLinked = false;
}
}
And where I call my browser initialization which is what JeffC recommended:
#Given("^I use \"([^\"]*)\" browser$")
public void browserInitialization(String browser) throws Throwable {
if (browser.equals("Chrome"))
{
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
driver = new ChromeDriver();
}
else if(browser.equals("Firefox"))
{
System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
driver = new FirefoxDriver();
}
else if(browser.equals("Edge"))
{
System.setProperty("webdriver.edge.driver", "C:\\Program Files (x86)\\Edge Driver\\MicrosoftWebDriver.exe");
driver = new EdgeDriver();
}
else if(browser.equals("IE11"))
{
System.setProperty("webdriver.ie.driver", "C:\\Program Files (x86)\\Internet Explorer\\IEDriverServer32.exe");
driver = new InternetExplorerDriver();
}
}
And on the cucumber side I use a scenario outline I just use a data table for browser grabbing:
|Browser|
|Chrome|
|FireFox|
|Edge|
|IE11|