Automate basic authentication
The image is used to change focus to the sign-in popup window.
As there is no way to target the popup window, use the surface-based Robotics driver to click the popup and provide credentials.
See the code below as an example:
import com.workfusion.odf2.compiler.BotTask;
import com.workfusion.odf2.core.task.AdHocTask;
import com.workfusion.odf2.core.task.TaskInput;
import com.workfusion.odf2.core.task.output.TaskRunnerOutput;
import com.workfusion.odf2.core.webharvest.rpa.RpaDriver;
import com.workfusion.odf2.core.webharvest.rpa.RpaFactory;
import com.workfusion.odf2.core.webharvest.rpa.RpaRunner;
import com.workfusion.rpa.driver.Driver;
import com.workfusion.rpa.helpers.RPA;
import com.workfusion.rpa.helpers.UiElement;
import com.workfusion.rpa.helpers.UiElementCollection;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import static com.workfusion.rpa.helpers.RPA.*;
import static com.workfusion.rpa.helpers.UiSelectors.byImage;
import static com.workfusion.rpa.helpers.UiSelectors.byText;
@BotTask(requireRpa = true)
public class AutomateWebElements implements AdHocTask {
private final RpaRunner rpaRunner;
private final Logger logger;
@Inject
public AutomateWebElements(RpaFactory rpaFactory, Logger logger){
this.rpaRunner = rpaFactory
.builder(RpaDriver.UNIVERSAL)
.closeOnCompletion(true)
.build();
this.logger = logger;
}
@Override
public TaskRunnerOutput run(TaskInput taskInput) {
rpaRunner.execute(driver->{
this.automateBasicAuthentication(driver);
});
return taskInput.asResult()
.withColumn("example_bot_task_output", "completed_successfully");
}
private void automateBasicAuthentication(Driver driver){
//imagePath is the directory where Authentication Pop image is stored that will be used to switch focus to that popup using the surface-based Robotics driver.
//User needs to create this image on their own and modify the imagePath accrodingly.
String imagePath = "C:\\Users\\sshukla\\Desktop\\New folder\\";
RPA.timeouts(40 * 1000);
RPA.openChrome("http://browserspy.dk/password.php");
$(By.xpath("//*[@id='right']/div/table/tbody/tr[1]/td/a")).click();
RPA.sleep(2000);
//As there is no way to target the popup window, use the surface-based Robotics driver to click the popup and provide credentials.
$(byImage(imagePath + "basicLogin.png", -50, 105)).click();
//Providing Credentials
RPA.sendKeys("test");
RPA.pressTab();
RPA.sendKeys("test");
RPA.pressTab();
RPA.pressEnter();
String status = $(By.xpath("//*[@id='right']/div/h1")).getText();
logger.debug(status);
this.popAlert(driver,status);
}
private void popAlert(Driver driver,String popupMessage){
driver.switchDriver("chrome");
//Switch to default content inorder to work with alert if already on a IFrame
RPA.switchTo().defaultContent();String script = popupMessage;
driver.executeScript("alert(arguments[0])",script);
RPA.sleep(3000);
RPA.switchTo().alert().accept();
}
}