2014年10月14日 星期二

robotframework demo 2 (selenium2library)

In  robotframework demo 1,  we define parameter.txt and key word.txt.  Follow those define, I desing  demo 2 to understand  Suite Setup、Suite Teardown 、Test Setup、Test Template

1. Create search_demo2 file

On Google_Demo, select this suite, right click on it and then click on New Suite

Enter  ->  search_demo2
Type ->   File
Format  ->  TXT

2. Edit search_demo2



 *** Settings ***
Suite Setup       Open Google web
Suite Teardown    Close Browser
Test Setup        Go to    ${Server}
Test Template     Input Search keyword
Resource          resource/keyword.txt

*** Test Cases ***    Keyword
keyword1              yahoo

keyword2              youtube

keyword3              python3

keyword4              ${KEYWORD}

keyword5              java

*** Keywords ***
Input Search keyword
    [Arguments]    ${key}
    Click Element    xpath=//*[@id="lst-ib"]
    Input Text    lst-ib    ${key}
    Press Key    xpath=//*[@id="lst-ib"]    \\27
    Click Button    xpath=//*[@id="tsf"]/div[2]/div[3]/center/input[1]
    Sleep    2


To Explain:

Suite Setup:  Start the test operation
Suite Teardown:   End the test operation
Test Setup:    Every test case start point
Test Template:   Every test case operation















3. Run search_demo2

robotframework demo 1 (selenium2library)

啟動 robotframework IDE


$ ride.py

1. Create a Google_Demo project
File -> New Project


Enter  ->  Google_Demo 
Type -> Directory
Format  ->  TXT

左邊視窗將會多一個 google demo 資料夾

2.  Create a New Suite (resource folder)

On Google_Demo, select this suite, right click on it and then click on New Suite

Enter  ->  resource
Type -> Directory
Format  ->  TXT

3. Create two Resource file  (parameter.txt and keyword.txt) 

On resource, select this suite, right click on it and then click on New Resource

Enter  ->  parameter
Format  ->  TXT

select resource again


Enter  ->  keyword
Format  ->  TXT

The Folder structure will like this picture



4. Edit parameter.txt ->  this file put parameter of your define
Content will like this


*** Settings ***
Documentation     A resource file with reusable keywords and variables.
...
...               The system specific keywords created here form our own
...               domain specific language. They utilize keywords provided
...               by the imported Selenium2Library.

*** Variables ***
${SERVER}         http://www.google.com.tw
#${BROWSER}       Firefox
${BROWSER}        Chrome
${DELAY}          0.5
${WIDTH}          1024
${HIGHT}          768
${KEYWORD}        robotframework

*** Keywords ***


To Explain :

${SERVER}     =>  you defind web
${BROWSER} =>  you open browser
${DELAY}       =>  For run speed seconds
${WIDTH}       =>  Defind you browser window size
${HIGHT}        =>  Defind you browser window size
${KEYWORD}  =>  Search Key word

5. Edit keyword.txt ->  this file put you define keywords


*** Settings ***
Library           Selenium2Library
Resource          parameter.txt

*** Keywords ***
Open Google web
    Open Browser    ${SERVER}    ${BROWSER}
    Set Window Size    ${WIDTH}    ${HIGHT}
    Set Selenium Speed    ${DELAY}


To Explain :

Open Google web  => you define key word

Open Browser 、 Set Window Size 、 Set Selenium Speed =>  selenium2library key word

PS: if you don't know selenium2library key words、 you cau press F5, will pop a window, you can search key word and how to use the key word.


6. Create search_demo1 file

On Google_Demo, select this suite, right click on it and then click on New Suite

Enter  ->  search_demo1
Type ->   File
Format  ->  TXT

7. Edit search_demo1


*** Settings ***
Resource          resource/keyword.txt

*** Test Cases ***
Search Test
    Open Google Web
    Click Element    xpath=//*[@id="lst-ib"]
    Input Text    lst-ib    ${KEYWORD}
    Press Key    xpath=//*[@id="lst-ib"]    \\27
    Click Button    xpath=//*[@id="tsf"]/div[2]/div[3]/center/input[1]
    Sleep    3
    [Teardown]    Close Browser


To Explain:

Open Google Web  =>  Define from keyword.txt


Clilck Element  =>  define from library and xpath you can open chrome browse and use F12 function   to  search xpath and copy xpath。


Input Text  =>  define from library,  lst-ib is the element id,  ${KEYWORD} define from parameter.txt


Press Key => use key board function, xpath same as Click Element, \\27 is ASCII code, 27 is keybord esc.

Click Button => use mouse click the search button










8. Run Search_demo1
$ pybot Search_demo1.txt

2014年10月7日 星期二

Install robot framework IDE (os ubuntu 14.04)

In ubuntu 14.04

$ sudo aptitude install python-pip -y
$ sudo pip install robotframework
$ sudo pip install robotframework-selenium2library
$ sudo aptitude install wx2.8-headers libwxgtk2.8-0 libwxgtk2.8-dev python-wxgtk2.8 python-wxversion -y
$ sudo pip install robotframework-ride

下載 chrome web driver http://chromedriver.storage.googleapis.com/index.html

choice your version and download it.

Extract the file and folder has a file chromedriver

copy chromedriver to /usr/bin


sudo cp chromedrive /usr/bin

啟動 ridy

$ ridy.py &

2014年8月19日 星期二

mysql to csv format

use command output talble for csv


mysql -uroot 
mysql>

 select yourfield from youtable   
 into outfile '/tmp/test.csv'   
 fields terminated by ',' optionally enclosed by '"' escaped by '"'   
 lines terminated by '\r\n';   



接下來在 /tmp/test.csv 就可以看到該檔案了
vim /tmp/tset.csv 

2014年7月24日 星期四

ubuntu install Oracle java

由於 ubuntu apt-get 中,是 openjdk ,若要 install Oracle java, please follow these step。

1. check you java version

$ java -version

If Oracle Java is used, the results should look like:

java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

Otherwise OpenJDK java will like:

java version "1.7.0_21"
OpenJDK Runtime Environment (IcedTea 2.3.9) (7u21-2.3.9-1ubuntu1)
OpenJDK 64-Bit Server VM (build 23.7-b01, mixed mode)


若不是請到 Oracle Java SE 此網站
Downloads java .tar.gz 檔。
   
Note: If installing the Oracle JRE in a cloud environment, accept the license agreement,  
download the installer to  your local client, and then use scp (secure copy) to transfer the file to
your cloud machines.
Make a directory for the JRE:

2. create jvm folder

$ sudo mkdir -p /usr/lib/jvm

Unpack the tarball and install the JRE:

3. $ sudo tar zxvf jre-7u65-linux-x64.tar.gz -C /usr/lib/jvm

The JRE files are installed into a directory called /usr/lib/jvm/jre-7u_version.
Tell the system that there's a new Java version available:

4. $ sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/jre1.7.0_65/bin/java" 1

If updating from a previous version that was removed manually, execute the above command  twice, because you'll get an error message the first time.

Set the new JRE as the default:

5. $ sudo update-alternatives --set java /usr/lib/jvm/jre1.7.0_65/bin/java

Make sure your system is now using the correct JRE. For example:

6. $ java -version

java version "1.7.0_65"
Java(TM) SE Runtime Environment (build 1.7.0_65-b17)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)



reference

2014年5月28日 星期三

Hibernate 學習筆記(一) 使用 Maven 建立 Hibernate EXAMPLE


1. 先在 eclipse create maven project

Choose File –> New –> Project. Select Maven-> Maven Project -> Next
-> choice "maven-archetype-quickstart" -> Next

Group Id: whatever
Artifact Id: test_hibernate
Package: com.wiki.common

Click Finish.

2.  在 eclipse 左方的 那欄 針對剛 create 的 project 按右鍵
New -> Source Folder -> src/main/resources
要置入一些 xml檔使用


3. 在 pom.xml  <dependencies> </dependencies> 中加入以下的 dependency

Modify the Maven’s pom.xml file, add support for Hibernate and MySQL.
Hibernate requires dom4j, commons-logging, commons-collections and cglib as dependency library, mysql-connector-java  add it.

    
    
     org.hibernate
     hibernate-core
     4.3.5.Final
    
    
     dom4j
     dom4j
     1.6.1
    
    
     commons-logging
     commons-logging
     1.1.3
    
    
     commons-collections
     commons-collections
     3.2.1
    
    
     cglib
     cglib
     3.1
    
    
     mysql
     mysql-connector-java
     5.1.30
    


4.  在 src/main/resources 底下 add two xml file :  Stock.hbm.xml  and  hibernate.cfg.xml


Stock.hbm.xml

<hibernate-mapping>
    <class catalog="stock" name="com.wiki.common.Stock" table="stock">
        <id name="stockId" type="java.lang.Integer">
            <column name="STOCK_ID">
            <generator class="identity">
        </generator></column></id>
        <property name="stockCode" type="string">
            <column length="10" name="STOCK_CODE" not-null="true" unique="true">
        </column></property>
        <property name="stockName" type="string">
            <column length="20" name="STOCK_NAME" not-null="true" unique="true">
        </column></property>
    </class>
</hibernate-mapping>


hibernate.cfg.xml


<?xml version="1.0" encoding="UTF-8"?>
< !DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password"></property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/stock</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="show_sql">true</property>
        <mapping resource="Stock.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>



 5. 在 src/main/java/com/wiki/common/ 新增 Stock.java   及  main.java


Stock.java
package com.wiki.common;

/**
 * Model class for Stock
 */
public class Stock implements java.io.Serializable {

 private static final long serialVersionUID = 1L;

 private Integer stockId;
 private String stockCode;
 private String stockName;

 public Stock() {
 }

 public Stock(String stockCode, String stockName) {
  this.stockCode = stockCode;
  this.stockName = stockName;
 }

 public Integer getStockId() {
  return this.stockId;
 }

 public void setStockId(Integer stockId) {
  this.stockId = stockId;
 }

 public String getStockCode() {
  return this.stockCode;
 }

 public void setStockCode(String stockCode) {
  this.stockCode = stockCode;
 }

 public String getStockName() {
  return this.stockName;
 }

 public void setStockName(String stockName) {
  this.stockName = stockName;
 }

}



main.java
package com.wiki.common;

import org.hibernate.Session;
import com.wiki.persistence.HibernateUtil;

public class main 
{
    public static void main( String[] args )
    {
        System.out.println("Maven + Hibernate + MySQL");
        Session session = HibernateUtil.getSessionFactory().openSession();
 
        session.beginTransaction();
        Stock stock = new Stock();
 
        stock.setStockCode("9999");
        stock.setStockName("法克康");
 
        session.save(stock);
        session.getTransaction().commit();
    }
}


6.  在 src/main/java/com/wiki/persistence 新增 HibernateUtil.java


HibernateUtil.java

package com.wiki.persistence;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class HibernateUtil {

    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {

        try {

            // Create the SessionFactory from hibernate.cfg.xml

            return new Configuration().configure().buildSessionFactory();

        }

        catch (Throwable ex) {

            // Make sure you log the exception, as it might be swallowed

            System.err.println("Initial SessionFactory creation failed." + ex);

            throw new ExceptionInInitializerError(ex);

        }

    }

    public static SessionFactory getSessionFactory() {

        return sessionFactory;

    }

    public static void shutdown() {

     // Close caches and connection pools

     getSessionFactory().close();

    }

}


7. 到 mysql 終端介面 create database

# mysql -uroot -p

mysql> create database stock;
mysql> use stock;

mysql> CREATE TABLE `stock` (
   `STOCK_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `STOCK_CODE` VARCHAR(10) NOT NULL,
  `STOCK_NAME` VARCHAR(20) NOT NULL,
  PRIMARY KEY (`STOCK_ID`) USING BTREE,
  UNIQUE KEY `UNI_STOCK_NAME` (`STOCK_NAME`),
  UNIQUE KEY `UNI_STOCK_ID` (`STOCK_CODE`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


8. Run main.java

若有成功看到這兩行,代表 data 已成功寫入 DB了
Maven + Hibernate + MySQL

Hibernate: insert into stock.stock (STOCK_CODE, STOCK_NAME) values (?, ?)




reference(參考來源)

2014年3月28日 星期五

架設伺服務器需要加參數


在建立多人連線時

可以加入以下參數

在多人連線時不讓速度變慢

$  sudo vim /etc/sysctl.conf

net.ipv4.tcp_syncookies=1
net.ipv4.tcp_tw_reuse=1
net.ipv4.tcp_tw_recycle=1
net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_keepalive_time=120

$ sudo sysctl -p

詳細參數還需要了解