How to make Selenium chrome web driver fast - selenium

#Before
public void setUpRestClient() throws InterruptedException {
try {
driver.manage().timeouts().implicitlyWait(200, TimeUnit.SECONDS);
List<String> indexSuburbStatePost = new ArrayList<String>();
indexSuburbStatePost.add("ABC");
indexSuburbStatePost.add("YYZ");
for (int j = 0; j < indexSuburbStatePost.size(); j++) {
System.out.println("for loop===>" + indexSuburbStatePost.get(j));
mySchoolDriver(indexSuburbStatePost.get(j));
Thread.sleep(10000);
List<String> indexNumberSchool = new ArrayList<String>();
List<String> indexNameSchool = new ArrayList<String>();
List<WebElement> elementsAdv = driver.findElements(By.xpath("//table[#id='SearchResults']/tbody/tr/td"));
System.out.println("Test advance elements number of elements: " + elementsAdv.size());
writeToFile(indexSuburbStatePost.get(j));
for (WebElement eleadv : elementsAdv) {
System.out.println("Text adv======>" + eleadv.getText());
if (eleadv.getText().equalsIgnoreCase("Primary")
|| eleadv.getText().equalsIgnoreCase("Secondary")
|| eleadv.getText().equalsIgnoreCase("Government")
|| eleadv.getText().equalsIgnoreCase("Combined")
|| eleadv.getText().equalsIgnoreCase("Special")
|| eleadv.getText().equalsIgnoreCase(
"Non-government")) {
} else {
indexNameSchool.add(eleadv.getText());
}
}
Iterator<String> indexNumberSchoolIteratorAA = indexNameSchool
.iterator();
for (int k = 0; k < indexNameSchool.size(); k++) {
System.out.println("indexNumberSchoolIterator AA===>"+ indexNumberSchoolIteratorAA.next());
writeToFile(indexNameSchool.get(k));
}
List<WebElement> elementscss = driver.findElements(By.cssSelector("#SearchResults tr a"));
for (WebElement e : elementscss) {
String url = e.getAttribute("href");
System.out.println(url.substring(url.length() - 5));
indexNumberSchool.add(url.substring(url.length() - 5));
}
for (int i = 0; i < indexNumberSchool.size(); i++) {
Thread.sleep(10000);
writeToFile(indexNumberSchool.get(i));
try {
driver.findElement(By.xpath("//a[#href=\"/Home/Index/"+ indexNumberSchool.get(i) + "\"]")).click();
driver.findElement(By.id("IAccept")).click();
driver.findElement(By.className("captch-submit")).click();
} catch (NoSuchElementException ex) {
do nothing, link is not present, assert is passed
System.out.println(" NoSuchElementException======>" + ex.getMessage());
mySchoolDriver(indexSuburbStatePost.get(j));
Thread.sleep(10000);
driver.findElement(By.xpath("//a[#href=\"/Home/Index/"+ indexNumberSchool.get(i) + "\"]")).click();
}
driver.findElement(By.id("NaplanMenuButton")).click();
try {
driver.findElement(By.xpath("//*[#id=\"NaplanMenu\"]/ul/li[2]/a")).click();
} catch (ElementNotVisibleException envex) {
System.out.println(" ElementNotVisibleException======>"+ envex.getMessage());
}
List<WebElement> elements = driver.findElements(By.xpath("//div[#id='ResultsInNumbersContainer']/table/tbody/tr"));
System.out.println("Test7 number of elements: " + elements.size());
BufferedWriter writer = null;
File f = null;
for (WebElement ele : elements) {
if (ele.getAttribute("class").equalsIgnoreCase( "selected-school-row")) {
writeToFile(ele.getText());
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
driver.close();
}
}
private void mySchoolDriver(String indxSuburbPost) {
driver.get("http://www.abc.xyz.com");
driver.findElement(By.id("SuburbTownPostcodeSearch")).sendKeys(indxSuburbPost);
driver.findElement(By.id("SuburbTownPostcodeSearchSubmit")).submit();
}
private void writeToFile(String indxSuburbPost) {
try{
String filename = "C:/logs/data.txt";
FileWriter fw = new FileWriter(filename, true);
fw.write(indxSuburbPost + "\n");
fw.write("\n");
fw.close();
}catch (Exception e) {
e.printStackTrace();
}
}
Hi Friends,
I have above code and it is working fine and getting the data which I need. My only problem is : it is very slow.
If you look at the code he place where it is very slow is where I am cathing errors:
catch (NoSuchElementException ex) AND
catch (ElementNotVisibleException envex)
it takes ages to error to get caught.Can some one please help.

It's because of the .implicitlyWait() set at 200s. When an element is being located and not found, it will wait for 200s. My suggestion is to remove the implicit wait (and Thread.sleep()s) and replace them with explicit waits using WebDriverWait and ExpectedConditions.
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(...)).click();
// as long as you are OK with the time setting in the above WebDriverWait declaration
// (10 seconds), you can reuse the wait again and again with the same 10s wait.
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(...));
wait.until(ExpectedConditions.elementToBeClickable(...)).click();
Read more about explicit and implicit waits and why you shouldn't mix them.

Related

jvm condition and locksupport which is faster?

An experimental example of synchronous call is made. Each thread task waits for a task callback with its own value of + 1. The performance difference between condition and locksupport is compared. The result is unexpected. The two times are the same, but the difference on the flame diagram is very big. Does it mean that the JVM has not optimized locksupport
enter image description here
public class LockerTest {
static int max = 100000;
static boolean usePark = true;
static Map<Long, Long> msg = new ConcurrentHashMap<>();
static ExecutorService producer = Executors.newFixedThreadPool(4);
static ExecutorService consumer = Executors.newFixedThreadPool(16);
static AtomicLong record = new AtomicLong(0);
static CountDownLatch latch = new CountDownLatch(max);
static ReentrantLock lock = new ReentrantLock();
static Condition cond = lock.newCondition();
static Map<Long, Thread> parkMap = new ConcurrentHashMap<>();
static AtomicLong cN = new AtomicLong(0);
public static void main(String[] args) throws InterruptedException {
long start = System.currentTimeMillis();
for (int num = 0; num < max; num++) {
consumer.execute(() -> {
long id = record.incrementAndGet();
msg.put(id, -1L);
call(id);
if (usePark) {
Thread thread = Thread.currentThread();
parkMap.put(id, thread);
while (msg.get(id) == -1) {
cN.incrementAndGet();
LockSupport.park(thread);
}
} else {
lock.lock();
try {
while (msg.get(id) == -1) {
cN.incrementAndGet();
cond.await();
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
latch.countDown();
});
}
latch.await();
consumer.shutdown();
producer.shutdown();
System.out.printf("park %s suc %s cost %s cn %s"
, usePark
, msg.entrySet().stream().noneMatch(entry -> entry.getKey() + 1 != entry.getValue())
, System.currentTimeMillis() - start
, cN.get()
);
}
private static void call(long id) {
producer.execute(() -> {
try {
Thread.sleep((id * 13) % 100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (usePark) {
msg.put(id, id + 1);
LockSupport.unpark(parkMap.remove(id));
} else {
lock.lock();
try {
msg.put(id, id + 1);
cond.signalAll();
} finally {
lock.unlock();
}
}
});
}
}

Multithread and Chronicle queue

so I'm trying to see what's the fastest way to write to Chronicle Queue in a multithread env so I have the following:
public static void main(String[] args) throws Exception{
final String path = args[0];
int times = Integer.parseInt(args[1]);
int num = Integer.parseInt(args[2]);
AtomicInteger nextid = new AtomicInteger(0);
ThreadLocal<Integer> id = ThreadLocal.withInitial(() -> nextid.getAndIncrement());
ChronicleTest test = new ChronicleTest();
ChronicleWriter writer = test.new ChronicleWriter(path);
CountDownLatch start = new CountDownLatch(1);
CountDownLatch done = new CountDownLatch(num);
Thread[] threads = new Thread[num];
long[] samples = new long[times * num];
for (int i = 0; i < num; i ++) {
threads[i] = new Thread(new Runnable() {
#Override
public void run() {
try {
start.await();
for (int i = 0; i < times; i++) {
int j = i + times*id.get().intValue();
long s = System.nanoTime();
writer.write(j + " DGirr5JgGVmxhvmaoO0c5MVVOUIEJxWa6nVStPnqmRl3T4hKE9tiwNjn6322uhgr2Fs4hDG8aKYvIB4O0733fx18EqGqNsshaSKoouky5ZekGK3vO87nfSUOz6uDD0olOp35QJQKPgr7tNlFgQP7BImcCyMPFCCm3yhSvOUgiVAD9W9BC3cqlKjQebOG4EkqzRIzwZjxbnIeK2YttfrvOvUJs0e9WBhXUVibi5Ks2j9ROQu2q0PJII4NYyN1a5YW2UKxyng3bRrtBVFqMSamtFzJ23EE4Y7rbQyeCVJhIKRM1LRvcGLUYZqKICWwDtOjGcbXUIlLLYiJcnVRZ4gNRvbFXvTL4XDjhD3uP5S8DjnkeAIBZcQ4VEUf30x65pTGLhWjOMV6jtiEQOWKB3nsuPMhcS1lP3wTQztViW7T8IsQlA5kvVAsnT5A7pojS1CffcYz4a2Rwqf9w6mhTPPZXgpDGArtThW3a69rwjsQxEY9aTwi0Pu0jlSAMmvOA158QFsSeJvLoJXILfocgjNEkj7iVcO0Rc6eC6b5EhJU3wv80EEHnORMXpQVuAuPyao7vJsx06TMcXf9t7Py4qxplVPhptIzrKs2qke2t5A8O4LQzq19OfEQsQGEjqHSbnfWXjfuntuFR8rV4VMyLZO1z3K7HmHtCEy14p5u0C0lj7vmtCnrOum0bnG2MwaAR7DJPIpOtwRObli5K5grv54AWnJyagpRX5e3eTEA8NAKO8oDZuLaoCvgavv9ciFrJcIDmyleVweiVrHs1fQXJoELzFpH4BmvzBuUjfZ8ORSIZsVuq4Hpls19GIA8opb1mSBt7MTifLPauo8WDWRoNi9DvjL4Z08Je6DvrhAFXasU2CMugg5EZ06OXexU17qnvxx2Vz9s9E5U50jDemySZ78KcZ6nqhQKIXUrkTktoEan2JXxI2NNSqEYifwly8ZO2MDquPe4J11rAcOqYp9y6Kb4NtFpNysM1evrLPvCx8oe");
long e = System.nanoTime();
samples[j] = e - s;
}
done.countDown();
} catch (Exception e){
e.printStackTrace();
}
}
});
}
for (int i = 0; i < num; i ++) {
try {
threads[i].start();
} catch (Exception e){
}
}
long startT = System.currentTimeMillis();
start.countDown();
done.await();
long endT = System.currentTimeMillis();
System.out.println("Time to complete [" + times + "] iteration in [" + (endT - startT) + " ms] and threads [" + num + "]");
System.out.println("#######");
for (int i = 0; i < times * num; i ++){
System.out.println(samples[i]);
}
}
private class ChronicleWriter {
SingleChronicleQueue m_cqueue;
ThreadLocal<ExcerptAppender> m_appender;
ChronicleWriter(String path ) {
m_cqueue = SingleChronicleQueueBuilder.binary(path).build();
m_appender = new ThreadLocal<ExcerptAppender>() {
protected ExcerptAppender initialValue() {
return m_cqueue.acquireAppender();
}
};
}
void write(String msg){
m_appender.get().writeText(msg);
}
}
And I ran with parameters:
path 2500 40
For some reason, this keeps crashing with core dump. What am I doing wrong? My disk has lots of disk space so that shouldn't matter. Thanks!!
If your program is crashing due to OutOfMemory error then
note that the disk space and the actual space used by the program may differ.
You may need to increase jvm heap size.
Refer below link to increase jvm heap size
What are the Xms and Xmx parameters when starting JVMs?
Or
Refer below link if you are running your program through eclipse
http://www.planetofbits.com/eclipse/increase-jvm-heap-size-in-eclipse/
I have tried your program with following version of chronicle-queue and it works fine.
<dependency>
<groupId>net.openhft</groupId>
<artifactId>chronicle-queue</artifactId>
<version>4.5.14</version>
</dependency>

Selenium, clicking a button and secound iteration of a loop failing

I have a test
public class SeleniumForh extends BaseTest {
private static List<String> FNR;
private static final int THREAD_COUNT = 1;
private static Set<String> THREADS = Collections.synchronizedSet(new HashSet<String>());
static {
List<String> fnr = new ArrayList<String>();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(SeleniumManntallskryss.class.getResourceAsStream("/fnr")));
String line;
while ((line = reader.readLine()) != null) {
fnr.add(line);
}
FNR = Collections.synchronizedList(fnr);
} catch (IOException e) {
e.printStackTrace();
}
}
public SeleniumForhandstemmer(String platform, String browserName, String browserVersion) {
super(platform, browserName, browserVersion);
}
#Parameterized.Parameters
public static LinkedList<String[]> getEnvironments() throws Exception {
LinkedList<String[]> env = new LinkedList<String[]>();
for (int i = 0; i <THREAD_COUNT; i++) {
env.add(new String[]{"ANY", "firefox", "35"});
}
return env;
}
#Test
public void testForhand() throws Exception {
try {
System.out.println(System.currentTimeMillis() + " - " + Thread.currentThread() + ": testForhand");
THREADS.add(Thread.currentThread().toString());
do {
System.out.println(System.currentTimeMillis() + " - " + Thread.currentThread() + ": THREADS.size() = " + THREADS.size());
Thread.sleep(5000);
} while (THREADS.size() < THREAD_COUNT);
System.out.println(System.currentTimeMillis() + " - " + Thread.currentThread() + ": testForhand - ready to run");
WebDriver driver = webDriverThreadLocal.get();
driver.get("https://kill");
Assert.assertTrue(driver.getTitle().equals("Ehh"));
WebElement element = driver.findElement(By.xpath("/html/body/div/div[3]/div/div[2]/div/div/div/a"));
element.click();
WebElement brukerid = driver.findElement(By.xpath("/html/body/form/input[1]"));
brukerid.clear();
brukerid.sendKeys(new CharSequence[]{"2333"});
WebElement logginnknapp = driver.findElement(By.cssSelector("input[value=\"Login\"]"));
logginnknapp.click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement rolle = driver.findElement(By.cssSelector("#event33"));
rolle.click();
for (int i = 0; i < 10; i++) {
System.out.println("Re nr " + (i + 1));
WebElement fo = driver.findElement(By.linkText("Ree"));
fo.click();
WebElement sted = driver.findElement(By.xpath("//*[#id=\"form:panel:tabell_dd_data\"]/tr[1]/td"));
sted.click();
takeScreenshot("nod");
WebElement velg = driver.findElement(By.xpath("//*[#id=\"form:panel:tabeller\"]/div/div[2]"));
velg.click();
WebElement fnrFane = driver.findElement(By.xpath("//*[#id=\"mand:manntallsSokForm:tabView\"]/ul/li[2]"));
fnrFane.click();
WebElement velger = driver.findElement(By.xpath("//input[#data-aft=\"fodselsnummer\"]"));
velger.clear();
String fnr = FNR.remove(0);
velger.sendKeys(fnr);
WebElement sokknapp = driver.findElement(By.xpath("//*[#id=\"mad:tabView:searchSSN\"]"));
sokknapp.click();
Thread.sleep(10000);
WebElement fnrTxt = driver.findElement(By.xpath("//*[contains(text(), '" + fnr + "')]"));
System.out.println("Fnr tekstelement: " + fnrTxt);
WebElement regStemme = driver.findElement(By.xpath("//*[#id=\"stemmegivning:form:markOffAdvance\"]"));
System.out.println("Knd: " + regStemme);
regStemme.click();
driver.findElement(By.xpath("//*[contains(text(), 'bd')]"));
driver.get("https://y");
driver.findElement(By.xpath("//*[contains(text(), 'Min side')]"));
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
System.out.println(System.currentTimeMillis() + " - " + Thread.currentThread() + ": testSimple - test finished");
}
}
}
The problem is that when running the secound iteratoion the button I want to click is just marked with a square around it in the running browser, not cliked. This happens everu time on the second (not the first iteration), I have tried sleep between iterations and have also confirmed that the button i sfound, so the problem is the click function. And I can see it tries to click because a square is apperaring around the buttoon.

how to get text from span in the below code

I want to get text from all the check boxes given in a drop down box in order to compare those values with db. Please help me. I am able to count the size by options.size(), but unable to get the text/strings to compare with each string in db.
//Click on Select Box
Thread.sleep(500);
driver.findElement(By.xpath(".//*[#id='DivZone']/div/button")).click();
Thread.sleep(500);
List<WebElement> options =
driver.findElements(By.xpath("//input[contains(#name,
'multiselect_ddlZone')]"));
//List<WebElement> options = driver.findElements(By.cssSelector(".ui-
corner-all.ui-state-hover>span"));
List<String> all_elements_text=new ArrayList<>();
if(region!=null)
{
for(int i=1; i<options.size(); i++)
{
all_elements_text.add(options.get(i).getText());
System.out.println(options.get(i).getText());
boolean isThere = false;
for (int j = 0; j < region.size(); j++)
{
if
(options.get(i).getText().equalsIgnoreCase(region.get(j))) {
// Code to display warning
isThere = true;
}
}
if(isThere)
System.out.println(options.get(i).getText()+" is matched
with Database data");
else
System.out.println(options.get(i).getText()+" is not matched
with Database data");
}
}
If unfortunately getText() doesn't work, you should try using getAttribute("textContent"‌​) as below :-
System.out.println(options.get(i).getAttribute("textContent"‌));
Or try using getAttribute("innerHTML"‌) as below :-
System.out.println(options.get(i).getAttribute("innerHTML"‌));
Right now, each "option" is an "input" WebElement, which will not have any text.
You may want to try searching for the list items, then pulling out the span content:
List<WebElement> options = driver.findElements(By.cssSelector("ul.ui-multiselect-checkboxes li label[for*='ddlZone'] span"));
Gave it a try on "www.tripadvisor.in", and able to make it (Used 'break' statement just to limit the loop in the example). Below is the code I tried on
public class SelectList {
WebDriver driver;
By currencyDrop = By.xpath(".//*[#id='CURRENCYPOP']/span");
By currency = By.xpath(".//*[#id='GLOBAL_CURRENCY_PICKER']/ul/li");
#Test
public void f() throws InterruptedException {
driver = new FirefoxDriver();
driver.get("https://www.tripadvisor.in/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(currencyDrop).click();
//Below is the list of options present in dropdown(per DB)
String[] expList = {"INR ₹ Indian Rupees",
"USD US$ U.S. Dollars",
"EUR € Euros"};
List <WebElement>countryList = driver.findElements(currency);
int i =0;
for(WebElement curr:countryList){
if (i>2){
break;
}
if (curr.getText().equals(expList[i])){
System.out.println("ActCurrency" + curr.getText());
System.out.println("ExpCurrency" + expList[i]);
i++;
}
}
}
}

I have not getting clear result about this issue

Am New to selenium Webdriver i have one project called Smoke testing .ie testing all the websites links are working fine or not .if any link is broken or not working means that test report sending email to the specified url.Actually i have write some codes for testing all the links and all the websites are getting from excel sheet problem is if the link is not working bt it shows the result as it is working nu so i cannot know the exact solution.here am attaching that codes Especially all the websites are made up of Wordpress.
public class LinktestTest
{
public static WebDriver driver;
#Test
public void findurl() throws Throwable, IOException {
FileInputStream fi = new FileInputStream("D:\\sample.xls");
Workbook wb = Workbook.getWorkbook(fi);
//driver = new FirefoxDriver();
Sheet s = wb.getSheet(0);
System.out.println("Program Started"+s.getName());
System.out.println(s.getRows());
for (int row = 0; row <= s.getRows()-1; row++)
{
driver = new FirefoxDriver();
String Urllist = s.getCell(0, row).getContents().toString();
System.out.println("Urllist" + Urllist);
driver.get(Urllist);
List<WebElement> linkElements = driver.findElements(By.tagName("a"));
String[] linkTexts = new String[linkElements.size()];
driver.manage().timeouts().implicitlyWait(20, TimeUnit.MINUTES);
int j = 0;
for (WebElement e : linkElements) {
linkTexts[j] = e.getText();
j++;
}
// test each link
for (String t : linkTexts) {
if (t != null && !t.isEmpty()) {
driver.findElement(By.linkText(t)).click();
String title;
title = driver.getTitle();
System.out.println("title is"+title);
if((title.contains("You are not authorized to view this page"))||(title.contains("404")) ||
(title.contains("408"))|| (title.contains("400")) || (title.contains("401"))
|| (title.contains("403"))||(title.contains("Page not found"))||(title.contains("500"))||(title.contains("error"))||(title.contains("503 Service Unavailable")))
{
System.out.println(t + " the link is not working");
} else {
System.out.println("\"" + t + "\"" + " is working.");
driver.navigate().back();
}
}
}driver.quit();
}
}
}
Instead of getting link text(s) and clicking, you can just retrieve the "href" attribute of each "a" tag elements. Since the purpose is just to navigate to each url and check the links by opening and getting title, I recommend this as the better way. Below is the updated code, that worked.
Note: I have just tweaked the existing code for solving it
#Test
public void findurl() throws Throwable, IOException {
FileInputStream fi = new FileInputStream("D:\\sites.xls");
Workbook wb = Workbook.getWorkbook(fi);
//driver = new FirefoxDriver();
Sheet s = wb.getSheet(0);
System.out.println("Program Started"+s.getName());
System.out.println(s.getRows());
for (int row = 0; row < s.getRows(); row++)
{
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
String Urllist = s.getCell(0, row).getContents().toString();
System.out.println("Url number "+(row+1)+": " + Urllist);
driver.get(Urllist);
List<WebElement> linkElements = driver.findElements(By.tagName("a"));
System.out.println("The number of links under URL number "+(row+1)+" is: "+linkElements.size());
String[] linkhrefs = new String[linkElements.size()];
int j = 0;
for (WebElement e : linkElements) {
linkhrefs[j] = e.getAttribute("href");
j++;
}
// test each link
int k=0;
for (String t : linkhrefs) {
try{
if (t != null && !t.isEmpty()) {
System.out.println("Navigating to link number "+(++k)+": '"+t+"'");
driver.navigate().to(t);
String title;
title = driver.getTitle();
System.out.println("title is: "+title);
if((title.contains("You are not authorized to view this page"))||(title.contains("404")) ||
(title.contains("408"))|| (title.contains("400")) || (title.contains("401"))
|| (title.contains("403"))||(title.contains("Page not found"))||(title.contains("500"))||(title.contains("error"))||(title.contains("503 Service Unavailable")))
{
System.err.println(t + " the link is not working because title is: "+title);
} else {
System.out.println("\"" + t + "\"" + " is working.");
//driver.navigate().back();
}
}else{
System.err.println("Link's href is null.");
}
}catch(Throwable e){
System.err.println("Error came while navigating to link: "+t);
}
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
}
System.out.println("-------------------------------------------------------------");
System.out.println("Closing the driver as the links under url: "+Urllist+" have been checked out");
driver.quit();
}
}