Tuesday, 27 June 2023

PlantUML: Add notes to fields in a class

PlantUML support to add notes to fields in a class.

 

Example

note right of Double::POSITIVE_INFINITY
	A constant holding the 
	positive infinity of type double.
end note

 

fieldNotes.txt

@startuml

class Double{

	'public static fields
	+{static} double POSITIVE_INFINITY = 1.0 / 0.0
	+{static} double NEGATIVE_INFINITY = -1.0 / 0.0
	+{static} double NaN = 0.0d / 0.0
	+{static} double MAX_VALUE = 0x1.fffffffffffffP+1023

	- double val
}

note right of Double::POSITIVE_INFINITY
	A constant holding the 
	positive infinity of type double.
end note

note left of Double::NEGATIVE_INFINITY
	A constant holding the negative infinity of type double.
end note

note right of Double::NaN
	A constant holding a Not-a-Number (NaN) value of type double.
end note

note left of Double::MAX_VALUE
	A constant holding the largest positive finite value of type double
end note

note right of Double::val
	actual primitive values
end note

@enduml

 

Above snippet generate below diagram.


 

 

  

Previous                                                    Next                                                    Home

Playwright: implement auto scroll

By customizing the mouse when event, we can customize the scrolling.

 

Signature

void wheel(double deltaX, double deltaY);

deltaX : number of pixels to scroll horizontally.

deltaY : number of pixels to scroll vertically.

 

 Example

for (int i = 0; i < 5; i++) {
	page.mouse().wheel(0, 1000);
	TimeUnit.SECONDS.sleep(2);
}

 

AutoScroll.java

package com.sample.app.miscellaneous;

import java.util.concurrent.TimeUnit;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class AutoScroll {

	public static void main(String[] args) throws InterruptedException {
		try (Playwright playwright = Playwright.create()) {
			Browser browser = playwright.chromium()
					.launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));
			Page page = browser.newPage();
			page.navigate("https://self-learning-java-tutorial.blogspot.com/2014/07/basic-java.html");

			for (int i = 0; i < 5; i++) {
				page.mouse().wheel(0, 1000);
				TimeUnit.SECONDS.sleep(2);
			}
		}
	}

}

 

 

Previous                                                 Next                                                 Home

Playwright: get collection of elements matched to a selector

Using ‘page.locator(classSelector).allTextContents()’ method, we can get an array of textContent values for all matching nodes.

 

Example

List<String> allParagraphs = page.locator(classSelector).allTextContents();

 

Find the below working application.

 

collectionOfElements.html

 

<html>
	<body>
		<p class="mypara">Paragraph1</p>
		<p class="mypara">Paragraph2</p>
		<p class="mypara">Paragraph3</p>
		<p class="mypara">Paragraph4</p>
	</body>
</html>

 

FileUtil.java

package com.sample.app.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

public class FileUtil {
	
	public static String resourceAsString(String resourceName) throws IOException {
		ClassLoader classLoader = FileUtil.class.getClassLoader();
		URL url = classLoader.getResource(resourceName);
		if (url == null) {
			return null;
		}

		URLConnection urlConnection = url.openConnection();

		urlConnection.setUseCaches(false);

		try (InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream())) {
			char[] buffer = new char[1048];
			StringBuilder builder = new StringBuilder();

			int count = -1;
			while ((count = inputStreamReader.read(buffer, 0, buffer.length)) != -1) {
				builder.append(buffer, 0, count);
			}

			return builder.toString();
		}

	}
}

GetAllTheMatchedElements.java

package com.sample.app.locators;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
import com.sample.app.util.FileUtil;

public class GetAllTheMatchedElements {
	public static void main(String[] args) throws IOException {
		try (Playwright playwright = Playwright.create()) {
			Browser browser = playwright.chromium()
					.launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));
			final String content = FileUtil
					.resourceAsString("miscellaneous" + File.separator + "collectionOfElements.html");

			Page page = browser.newPage();
			page.setContent(content);

			String classSelector = "p[class=\"mypara\"]";
			List<String> allParagraphs = page.locator(classSelector).allTextContents();

			System.out.println("allParagraphs : " + allParagraphs);
		}
	}
}

Output

allParagraphs : [Paragraph1, Paragraph2, Paragraph3, Paragraph4]

 

 

Previous                                                 Next                                                 Home

Saturday, 24 June 2023

PlantUML: use HTML tags while defining notes

You can use following HTML tags while defining the notes.

 

a.   <b>

b.   <u>

c.    <i>

d.   <s>, <del>, <strike>

e.   <font color="#AAAAAA"> or <font color="colorName">

f.     <color:#AAAAAA> or <color:colorName>

g.   <size:nn> to change font size

h.   <img src="file"> or <img:file>: the file must be accessible by the filesystem

 

htmlNotes.txt

@startuml

interface List{
	
}

abstract class AbstractList implements List{
	
}

interface Serializable{
	
}

class ArrayList extends AbstractList implements Serializable{
	
}

note top of List 
An <b>ordered collection</b> (also known as a <i>sequence</i>). 
The user of this interface has precise control 
over where in the list each element is inserted.
end note

note left of AbstractList
This class provides a skeletal <b><font color="#FF0000"> implementation of the List interface </font></b> 
to minimize the effort required to implement this interface backed by 
a <u>random access</u> data store (such as an array).
end note


note right of ArrayList : <size:35> Resizable-array </size> implementation \n of the List interface
note bottom of Serializable : Serializability of a class is enabled \n by the class implementing this

@enduml

 

Above snippet generate below diagram.


 

  

Previous                                                    Next                                                    Home

Wednesday, 21 June 2023

PlantUML: Define note in several lines

 

You can define multiline notes in two ways.

a.   Using end note keyword

b.   Using \n

 

Example 1

note top of List 
An ordered collection (also known as a sequence). 
The user of this interface has precise control 
over where in the list each element is inserted.
end note

Example 2

note right of ArrayList : Resizable-array implementation \n of the List interface

 

multiLineNotes.txt

@startuml

interface List{
	
}

abstract class AbstractList implements List{
	
}

interface Serializable{
	
}

class ArrayList extends AbstractList implements Serializable{
	
}

note top of List 
An ordered collection (also known as a sequence). 
The user of this interface has precise control 
over where in the list each element is inserted.
end note

note left of AbstractList
This class provides a skeletal implementation of the List interface 
to minimize the effort required to implement this interface backed by 
a "random access" data store (such as an array).
end note


note right of ArrayList : Resizable-array implementation \n of the List interface
note bottom of Serializable : Serializability of a class is enabled \n by the class implementing this

@enduml

Above snippet generate below diagram.



  

Previous                                                    Next                                                    Home

Tuesday, 20 June 2023

Playwright: How to get the current page url?

Using ‘page.url()’ method, we can get the current page url.

 

GetCurrentPageURL.java

package com.sample.app.miscellaneous;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class GetCurrentPageURL {
	
	public static void main(String[] args) {
		try (Playwright playwright = Playwright.create()) {
			Browser browser = playwright.chromium().launch();
			Page page = browser.newPage();
			page.navigate("https://self-learning-java-tutorial.blogspot.com/");
			System.out.println(page.url());
			
		}
	}

}

 

Output

https://self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html

  Since the url "https://self-learning-java-tutorial.blogspot.com/” is redirected to “https://self-learning-java-tutorial.blogspot.com/2014/02/blog-post.html”, you can see this in the output.

 

Previous                                                 Next                                                 Home

Playwright: get all the titles in google search result

In this post, I am going to explain how to print all the website titles from google search result.

 


Find the below working application.

 

GoogleSearchResult.java

package com.sample.app.miscellaneous;

import java.nio.file.Paths;
import java.util.List;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class GoogleSearchResult {

	public static void main(String[] args) {
		try (Playwright playwright = Playwright.create()) {
			Browser browser = playwright.chromium()
					.launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(100));

			Page page = browser.newPage();
			page.navigate("https://google.com");

			page.fill("input[name=q]", "self learning java blogspot");
			page.click("input[value=\"Google Search\"]");

			page.waitForNavigation(() -> {
				System.out.println("Waiting for search result\n");
			});

			List<String> allTitles = page.locator("h3[class=\"LC20lb MBeuO DKV0Md\"]").allTextContents();
			for (String title : allTitles) {
				System.out.println(title);
			}

			String filePath = "/Users/Shared/playwright/search.png";
			page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get(filePath)));

		}
	}

}

 

Output

Waiting for search result

Programming for beginners
Self-learning-java-tutorial.blogspot.com - Feedreader.com
self-learning-java-tutorial.blogspot.com - Programming ... - Sur.ly
Learn Java from Basic to Advanced: Preface
self-learning-java-tutorial.blogspot.in
Make & Know Java | A Javaonfly production
Blogger API: Getting Started - Google Developers
Self-learning-java-tutorial.blogspot.com - HypeStat
Top 5 Free Java Courses for Beginners to Learn Online in 2023

 

 

Previous                                                 Next                                                 Home