Sunday, June 21, 2020

Robo Fusion: Vara veena mrudu paani song on piano with selenium automation. Selenium Code

When you cannot cover it manually, AUTOMATE it !
Love carnatic music. Love automation.
Robo Fusion: Vara veena mrudu paani song on piano with selenium automation.
వరవీణా మృదుపాణీ, వనరుహలోచన రాణీ..
#piano #Selenium #automation #varaveena #carnaticmusic


Main script:

package playPiano;

import org.openqa.selenium.WebDriver;

public class playPianoDriver {
public static void main(String[] args) throws Exception {
//Array (Octave:0/1/2/3/4)(note)(timeunit)
String sng[]= {"1e","1e","1g2","1g2","1a","1g","2c2","2c2","2d","2c","1a","1a","1g2","1a","1g","1e","1e","1d2","1e","1g","1a","2c","1a2","1a","1g","1e","1e","1d2","1e","1e","1a","1g","1e2","1g","1e","1e","1d","1c2","1e","1e","1e","1e","1d","1e","1g","1e","1g2","1g2","1e","1e","1a","1g","1a2","1a","1g","2c2","2c2","1a","2e","2d","2d","2c","2c","1a","2c","1a","1a","1a","1g","1e","1g","1a","2c","1a","1g","1a","1g","1e","1e","1d","1c","1c","1e","1e2","1e2","1e","1d","1g","1e","1d2","1c","1d","1c","1e","1d","1c","1d","1d","1c2","1c2"};
WebDriver drv=playPianoFuncs.launchApplication(keyConstants.url);
 
playPianoFuncs.playPiano(drv, sng);
 
}

}





Function library:

package playPiano;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class playPianoFuncs {
//launch application
public static WebDriver launchApplication(String url)
{
WebDriver drv=null;
 
System.setProperty("webdriver.chrome.driver", keyConstants.chromeDriverPath);
 
// Initialize browser
drv=new ChromeDriver();
// Open url
drv.get(url);
drv.manage().timeouts().implicitlyWait(keyConstants.commonTimeOut,TimeUnit.SECONDS);
// Maximize browser
drv.manage().window().maximize();
drv.manage().timeouts().implicitlyWait(keyConstants.commonTimeOut, TimeUnit.SECONDS);
return drv;
}
//get key numbers
public static int getKeyNumbers(int octave,String Keyname)
{
int keyNum=0;
switch (Keyname) 
{
//White keys
case "c": keyNum=keyConstants.c[octave];break;
case "d": keyNum=keyConstants.d[octave];break;
case "e": keyNum=keyConstants.e[octave];break;
case "f": keyNum=keyConstants.f[octave];break;
case "g": keyNum=keyConstants.g[octave];break;
case "a": keyNum=keyConstants.a[octave];break;
case "b": keyNum=keyConstants.b[octave];break;
//Black keys
case "C":
case "db": keyNum=keyConstants.db[octave];break;
case "eb": keyNum=keyConstants.eb[octave];break;
case "gb": keyNum=keyConstants.gb[octave];break;
case "ab": keyNum=keyConstants.ab[octave];break;
case "bb": keyNum=keyConstants.bb[octave];break;
}
return keyNum;
}

//play piano
public static void playPiano(WebDriver drv,String[] sng) throws InterruptedException
{
for (String string : sng) 
{
int octave=Integer.parseInt(string.substring(0, 1));
string=string.substring(1);
int playTime=keyConstants.kaalam1;
if(string.length()==2) {playTime=keyConstants.kaalam2;string=string.substring(0, 1);}
//System.out.println(string);
int ky=playPianoFuncs.getKeyNumbers(octave,string);
//System.out.println("Key number:"+ky);
WebElement key=drv.findElement(By.cssSelector("*["+keyConstants.keyboardAttribute+"='"+ky+"']"));
key.click();
TimeUnit.MILLISECONDS.sleep(playTime);
}
}
}




Constants File:

package playPiano;

public class keyConstants {
//Octave (Low-0, Medium-1, High-2)
//public static int LMH=1;
//Tempo
public static int kaalam1=500;
public static int kaalam2=1000;
//Low,Mid,High - White Keys
public static int c[]= {90,84,221};
public static int d[]= {88,89,86};
public static int e[]= {67,85,66};
public static int f[]= {81,73,78};
public static int g[]= {87,79,77};
public static int a[]= {69,80,188};
public static int b[]= {82,219,190};
//Low,Mid,High - Black Keys
public static int db[]= {83,54,70};
public static int eb[]= {68,55,71};
public static int gb[]= {50,57,74};
public static int ab[]= {51,48,75};
public static int bb[]= {52,187,76};
//driver and browser settings
public static String chromeDriverPath="C:\\AEM Migration Project\\Eclipse\\chromedriver.exe";
public static String url="https://www.musicca.com/piano";
//time parameters
public static int commonTimeOut=120;
public static int httpConnTimeOut=120000;
//Piano specific attribute
public static String keyboardAttribute="data-key";
}